這是 html:
<select name="name1" id="id1" class="class1" size="2" ondblclick="moveoption(this.id,'atmSelezionati',this.selectedIndex);" tabindex="40">
<option value="12345|0101">12345/0101</option>
<option value="64534|0102">64534/0102</option>
我試圖通過僅包含作為值標簽一部分的“0101”來選擇選項元素。
這就是我正在嘗試的,但它說 "unable to locate element":
driver.find_element_by_xpath("//*[contains(option/@value,'" "|" '0101' "')]")
uj5u.com熱心網友回復:
這個下拉選單是使用 select 和 option 標簽構建的,請使用Selenium Select 類:
select = Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "id1"))))
select.select_by_value('12345|0101')
進口:
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
更新 :
index_selection = 1
driver.execute_script(f"return document.getElementById('id1').selectedIndex = '{index_selection}';")
uj5u.com熱心網友回復:
解決方案:
我找到了這個解決方法:
driver.find_element_by_xpath("//option[contains(text(),'" "/" 'your text' "')]")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313840.html
上一篇:使用SeleniumAttributeError將href鏈接附加到串列中:“str”物件沒有屬性“get_attribute”
下一篇:如何從頁面中提取資訊
