[英]Python Selenium webdriver get XPATH and select dropdown
我用這段代碼在 HTML 表中找到了“Burger”這個詞
findRow = driver.find_element(By.XPATH, "//*[contains(text(),'Burger')]").value_of_css_property('#name')
HTML代碼
<tbody>
<tr>
<td>..</td>
<td>.....</td>
</tr>
<tr>
<td>Burger</td>
<td>
<select class="form-control input-sm" id="sel222" name="sel222" type="68" group="433" onchange="count(this)">
<option value="1">Vegetables</option>
<option value="2">Fish</option>
<option value="3">Beef</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-primary" id="submit"><span class="fa fa-save"></span> Save</button>
在這種情況下,您可以使用 xpath 以下技術識別 select 列表框。 使用下面的xpath識別select object
//td[contains(.,'Burger')]/following::select
您可以使用以下 XPath select 選擇Burger
:
.//td[text()='Burger']
您可以使用以下 XPath select select
標簽內的選項:
.//td[text()='Burger']//parent::tr//select/option
根據發布的 HTML,找到“Burger”的最簡單的 XPath 是,
//td[text()='Burger']
在“漢堡”單元格右側的單元格中找到 SELECT 的 XPath 將是,
//td[text()='Burger']/following-sibling::td/select
^ the XPath to find the TD that contains "Burger", from above
^ then find the sibling TD
^ then the SELECT child of the sibling
將所有這些放在“漢堡”單元格旁邊的 SELECT 元素中的 select“魚”,然后單擊提交,
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.XPATH, "//td[text()='Burger']/following-sibling::td/select"))
select.select_by_visible_text("Fish")
driver.find_element(By.ID, "submit").click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.