我正在尝试设置一个机器人来登录我的帐户,但是我似乎找不到使用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"]"}
答案 0 :(得分:1)
第一次尝试有两个问题
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 up
和login
,因此您将获得第一个元素(sign up
)。您应该使用第一种方法。
答案 1 :(得分:0)
我能够使用以下方式找到导航栏元素:
//ul[@class="navbar__links"]
注意:您在xpath中使用“ //”时只有一个“ /”