【问题标题】:Selenium - Not able to find Button Element in HTML codeSelenium - 无法在 HTML 代码中找到按钮元素
【发布时间】:2021-01-22 17:33:53
【问题描述】:

我刚刚开始学习 Selenium,并尝试在二手服装网站 vinted.fr 上自动搜索。

我能够在 HTML 代码中找到所有必要的元素,除了一个:
-将“排序依据”功能更改为“最新”的按钮。 ( This is a Screenshot of the Page including the Button I want to press)


现在我想知道问题可能是什么:
-是否有可能,该按钮在 HTML 代码中没有“按钮”标签。如果是,我如何找到并单击此元素?
-我只是在错误的位置搜索吗?


这是我建议按钮元素所在的 HTML 源代码:

【问题讨论】:

  • 请在您的帖子中添加 Html 作为文本并发布您的尝试。

标签: python selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

要单击元素以将 排序方式 功能设置为 最新,您需要为 WebDriverWait 引入 element_to_be_clickable(),您可以使用以下任一 @ 987654323@:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.pile li:nth-of-type(4) label"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pile']//li[@class='pile__element'][last()]//label"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多