簡體   English   中英

Selenium NoSuchElementException下拉菜單

[英]Selenium NoSuchElementException Drop Down Menu

如何在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相當陌生,因此任何建議都將對您有所幫助。

提前致謝!

我想您需要使用導入的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')

..如果dropdown元素是HTML select元素。

請確保您要查找的元素可見。 硒不能作用於不可見元素。 如果所需元素位於下拉菜單中,則首先需要將菜單下拉。 然后,嘗試再次找到該元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM