簡體   English   中英

Xpath python 包含值

[英]Xpath python contains value

這是 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>

我正在嘗試 select 選項元素,例如僅包含值標簽的一部分“0101”。

這就是我正在嘗試的,但它說"unable to locate element":

driver.find_element_by_xpath("//*[contains(option/@value,'"+ "|" + '0101' +"')]")

此下拉菜單是使用 select 和選項標簽構建的,請使用Selenium Select class

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}';")

解決方案:

我找到了這個解決方法:

driver.find_element_by_xpath("//option[contains(text(),'"+ "/" + 'your text' +"')]")

暫無
暫無

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

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