【问题标题】:Selenium NoSuchElementException Drop Down MenuSelenium NoSuchElementException 下拉菜单
【发布时间】:2019-03-26 17:17:33
【问题描述】:

如何在 Python 中使用 Selenium 绕过 NoSuchElementException 错误消息?我正在尝试从下拉菜单中选择报告类型,并在运行此代码后:

 from selenium.webdriver.support.select import Select

 driver = webdriver.Chrome()

 driver.get("https://examplehtml.com/gims/app/reports")

 ##Report type

 driver.find_element_by_xpath('//*[@id="reportType"]').send_keys("Power 
 Report")

这会插入单词“Power Report”,但它不会像我手动选择报告类型那样选择和向前移动页面,我认为这是因为 NoSuchElementException 错误。为什么找不到元素以及如何解决此错误。我对 Selenium 相当陌生,所以任何建议都会有所帮助。

提前致谢!

【问题讨论】:

  • 添加您要查找的元素的 html ?
  • 不幸的是,我无法共享 html,因为该网站的访问受到限制@Navarasu

标签: java python selenium web-crawler nosuchelementexception


【解决方案1】:

我猜你需要使用你导入的 Select 类。 因此,如果您的 HTML 如下所示:

<select>
  <option value="1">Power Report</option>
  <option value="2">Other Report</option>
</select>

.. 那我试试这段代码:

e = driver.find_element_by_xpath('//*[@id="reportType"]')
Select(e).select_by_value('1')

.. 如果下拉元素是 HTML 选择元素。

【讨论】:

  • 我试过你的方法,但它对我不起作用。我也尝试过“SendKeys”并使用“click”方法,但在使用其中任何一个时都会出现“Unresolved reference”错误。
  • 我刚刚更新了我的原始帖子。您需要在 select_by_value 函数中使用 option 元素的 value 属性。如果这不起作用,那么我猜,没有你的 HTML,我们将无法做更多@Jelena
【解决方案2】:

请确保您尝试查找的元素是可见的。 Selenium 不能对不可见的元素起作用。 如果您想要的元素在下拉菜单中,首先您需要将菜单下拉。然后,尝试再次找到该元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2021-04-02
    相关资源
    最近更新 更多