我找不到使用Selenium点击登录的方法

2024-04-18 13:45:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置一个bot来登录我的帐户,但是我似乎找不到使用Python和Selenium的方法。你知道吗

以下是网站:www.shopee.sg你知道吗

我试过悬停操作,找到xpath。但似乎都不管用。你知道吗

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

url = 'https://shopee.sg/'

driver = webdriver.Chrome('E:/users/Asashin/Desktop/Bots/others/chromedriver.exe')
driver.get(url)

actions = ActionChains(driver)
wait = WebDriverWait(driver, 10)

driver.find_element_by_xpath('//button[text()="English"]').click()

time.sleep(2)

driver.find_element_by_class_name('shopee-popup__close-btn').click()

time.sleep(2)

firstLevelMenu = driver.find_element_by_xpath('/ul[@class="navbar__links"]')
action.move_to_element(firstLevelMenu).perform();

secondLevelMenu = driver.find_element_by_xpath("//a[contains(text(),'Login')]");
secondLevelMenu.click();

以下是它未能执行或找到的一些程序:

actions.move_to_element(knownElement, 10, 25).click().build().perform()
actions.click()
actions.perform()
nav = driver.find_element_by_css_selector('li["navbar__link navbar__link--account navbar__link--tappable navbar__link--hoverable navbar__link-text navbar__link-text--medium"]')
a = driver.findElement(By.linkText('Login'))
actions.move_to_element(nav).moveToElement(a)
time.sleep (1)
actions.click(a)
actions.perform()

nav = driver.find_element_by_css_selector('li["navbar__link navbar__link--account navbar__link--tappable navbar__link--hoverable navbar__link-text navbar__link-text--medium"]')
actions.move_to_element(nav).perform()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[login]"))).click()                                       
continue_link = driver.find_element_by_link_text('Login')

actions.move_to_element(continue_link)
actions.click(continue_link)
actions.perform()

driver.find_element_by_link_text("Login").click()

driver.find_element_by_xpath('//div[@class="navbar-wrapper container-wrapper"]//div[@class="container navbar"]/ul[@class="navbar__links"]//li[@class="navbar__link navbar__link--account navbar__link--tappable navbar__link--hoverable navbar__link-text navbar__link-text--medium"]//li[contains(text(),"Login")]').click(

我希望它会响。但他们都出现了这样的错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/ul[@class="navbar__links"]"}

这总是它的结果:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/ul[@class="navbar__links"]"}

Tags: totextimportactionsbydriverseleniumlink
2条回答

在第一次尝试中有两个问题

xpath '/ul[@class="navbar__links"]'中缺少/,应该是'//ul[@class="navbar__links"]'

登录按钮有<li>标记,而不是<a>标记,应该是'//li[contains(text(), "Login")]'

firstLevelMenu = driver.find_element_by_xpath('//ul[@class="navbar__links"]')
action.move_to_element(firstLevelMenu).perform();

secondLevelMenu = driver.find_element_by_xpath('//li[contains(text(), "Login")]');
secondLevelMenu.click();

在第二次尝试中,您使用的是无效的css_selector,您没有提到class属性

nav = driver.find_element_by_css_selector('li[class="navbar__link navbar__link account navbar__link tappable navbar__link hoverable navbar__link-text navbar__link-text medium"]')

但它会导致两个元素,即sign uplogin,因此您将得到第一个元素(即sign up)。你应该使用第一种方法。你知道吗

我可以通过以下方式找到导航条元素:

//ul[@class="navbar__links"]

注意:您只有一个“/”,而我用“/”表示xpath

相关问题 更多 >