簡體   English   中英

Python 3 Selenium Select 來自具有相同 ID 但值不同的下拉菜單

[英]Python 3 Selenium Select from drop down menu with the same ID but different value

我正在使用 Firefox 並想從下拉菜單中單擊所選文本。 於是我打開了optgroup,想選這個:

<option value="WSS" id="A5">[PREMIUM] WSS (wss://)</option>

有不同的值,只是具有另一個值,但 ID 相同。

我試過這個但沒有成功:

select = Select(driver.find_element_by_id('A5'))
select.select_by_value("WSS")
select.select_by_visible_text("visible text/the shown text on the drop down menu")
select.select_by_value('WSS') 

您可以嘗試在 HTML 代碼中獲取必須是“選擇”的父 object(帶有“./..”)並列出所有可能的選項。 它避免了您的多個 id 的問題。

嘗試這個:

yourValue = "WSS"
listOptions = driver.find_elements_by_xpath("//option[@id='A5']/./../option")
for option in listOptions:
    if option.text == yourValue: # if wrong test "if option.get_attribute("innerText") == yourValue:"
        indexed = listOptions.index(option)
        break
Select(driver.find_elements_by_xpath("//option[@id='A5']/./..]")).select_by_index(indexed)

我直接寫在這里,它可能是錯誤的,不要猶豫,用更多的 HTML 代碼給你的反饋,它會有所幫助;)

暫無
暫無

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

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