找不到含硒元素

2024-06-26 17:46:22 发布

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

我正在尝试为Etsy shop manager创建一个刮板,但在登录后,我无法使用代码找到shop manager按钮(NoTouchElement)。这是我的密码:

from selenium import webdriver


options = webdriver.ChromeOptions()
options.add_argument('--incognito')

driverLocation = 'Downloads/chromedriver'

driver = webdriver.Chrome(driverLocation)

driver.get("https://www.etsy.com/signin")
username = driver.find_element_by_id("join_neu_email_field")
username.clear()
username.send_keys("username")

password = driver.find_element_by_id("join_neu_password_field")
password.clear()
password.send_keys("password")

driver.find_element_by_name("submit_attempt").click()

driver.implicitly_wait(10)

driver.find_element_by_class_name("wt-display-inline-flex-xs wt-flex-direction-column-xs wt-align-items-center wt-text-link-no-underline wt-p-xs-2 wt-pt-lg-0 wt-pb-lg-0 wt-pl-lg-1 wt-pr-lg-1 ge-nav-link reduced-margin-xs").click()

driver.quit()

我试图在网站上找到的按钮:

The button I am trying to locate on the website

我尝试过各种不同的方法(xpath、类名、css选择器)。我只把类名看作一个标识符,所以我不知道为什么它不起作用


Tags: bydriverusernamemanagerpasswordelementfindshop
3条回答
     driver.find_element_by_class_name("wt-display-inline-flex-xs.wt-flex-direction-column-xs.wt-align-items-center.wt-text-link-no-underline.wt-p-xs-2.wt-pt-lg-0.wt-pb-lg-0.wt-pl-lg-1.wt-pr-lg-1.ge-nav-link.reduced-margin-xs").click()

类中的空格表示有多个类,必须用点替换空格

否则使用xpath或css

   driver.find_element_by_xpath("//*[@class=\"wt-display-inline-flex-xs wt-flex-direction-column-xs wt-align-items-center wt-text-link-no-underline wt-p-xs-2 wt-pt-lg-0 wt-pb-lg-0 wt-pl-lg-1 wt-pr-lg-1 ge-nav-link reduced-margin-xs\"]").click()



   driver.find_element_by_cssSelector("[class=\"wt-display-inline-flex-xs wt-flex-direction-column-xs wt-align-items-center wt-text-link-no-underline wt-p-xs-2 wt-pt-lg-0 wt-pb-lg-0 wt-pl-lg-1 wt-pr-lg-1 ge-nav-link reduced-margin-xs\"]").click()

这将选择具有精确值的属性类的元素

您不应该在这里使用类名,因为这个类看起来非常动态。 也不建议在xpath中使用href属性值

  1. 在这种情况下,您应该针对具有静态属性或文本的某个元素编写xpath。我觉得span标记中有一个文本,所以您可以在这里尝试-//span[text()='putchetextvalue']/parent::a

  2. 在与此元素交互之前等待5秒,以确保在查找此元素时已加载该元素。以后你可以调整这个

<a>元素有一个子元素<span>。因此,要单击该元素,可以使用以下任意一种Locator Strategies

  • 使用css_selector

    driver.find_element_by_css_selector("a[href^='https://www.etsy.com/your/shops/me/dashboard'] > span").click()
    
  • 使用xpath

    driver.find_element_by_xpath("//button[@class='save' and text()='save']").click()
    

理想情况下,要单击元素,您需要为element_to_be_clickable()诱导WebDriverWait,并且可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='https://www.etsy.com/your/shops/me/dashboard'] > span"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@href, 'https://www.etsy.com/your/shops/me/dashboard')]/span"))).click()
    
  • 注意:您必须添加以下导入:

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

相关问题 更多 >