簡體   English   中英

NoSuchElementException:消息:沒有這樣的元素:在嘗試選擇下拉框中的選項時無法找到元素

[英]NoSuchElementException: Message: no such element: Unable to locate element while trying to select options within a drop down box

我正在嘗試創建一個腳本,該腳本可以自動單擊下拉框並單擊我想要的選項。 我嘗試從另一個類似的問題實現代碼,但是收到錯誤消息。

使用類似問題的解決方案,我嘗試了以下代碼行:

driver.find_element_by_xpath("//select[@name='interface']/option[text()='Management']").click()

的HTML

<select class="col-1 custom-select" name="interface" id="interface" required="required">
  <option selected="" disabled="" class="hideoption">Select Interface</option>
  <option value="InterfaceLAN">Production</option>
  <option value="MgmtLAN">Management</option>
  <option value="Clustering">Clustering</option>
</select>

我想自動化單擊下拉框並選擇“管理”選項的過程。 但是,我收到一條錯誤消息,如下所示:

NoSuchElementException: Message: no such element: Unable to locate element:"method":"xpath","selector":"//select[@name='interface']/option[text()='Management']"}

由於所需元素是一個select元素,因此您需要使用Select類並誘導WebDriverwait使所需元素可見,並且可以使用以下解決方案:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# other lines of code
select = Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//select[@id='interface' and @name='interface']"))))
select.select_by_value("MgmtLAN")

嘗試使用Select Class,在那里提供下拉菜單的xpath。 然后嘗試通過值,索引或可見文本選擇下拉列表

碼:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_name('name'))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)

暫無
暫無

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

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