python - 使用 selenium webdriver xpath 选择组合框

标签 python selenium xpath selenium-webdriver webdriver

我需要能够在组合框中选择一个元素,使用 xpath 似乎是做到这一点的方法。我找到了路径和盒子的ID,它是

Path = div/div/div
ID = stuff_devicessettings_Modbus_TCP

我尝试了几种不同的方法来打开盒子,这是我失败的一些尝试

browser.find_element_by_xpath("//div/div/div[contains(text(),'No Device Connected')]").click()
browser.find_element_by_xpath("//div/div/div[@id ='stuff_devicessettings_Modbus_TCP']//div/div/div[text()=""No Device Connected"])

但是都没有成功,请帮我找到一种打开并从组合框中选择元素的方法。

这是包含的内容

<select data-bind="uid: uid + '_device', options: devices, optionsText: 'name', optionsCaption:     
strings.notConnected, optionsAfterRender: applyDeviceAvailability, value: selectedDevice,  
enable: 
canModifyDevice" id="stuff_devicessettings_Modbus_TCP"><option value="">No device 
connected</option><option value="">Create a new device</option></select>

最佳答案

Selenium 有一种特殊的方式来处理 select->option 元素 - a Select class 。通过 id 找到您的 select 标签,实例化一个 Select 类和 select an option by visible text :

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('stuff_devicessettings_Modbus_TCP'))
select.select_by_visible_text("No device connected")

关于python - 使用 selenium webdriver xpath 选择组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107688/

相关文章:

python - 经过一段时间后如何更新显示的元素?

java - Selenium WebDriver 无法通过@FindBy 注释找到元素

javascript - 如何使用 selenium 和 PhantomJS 从动态网站中提取值

python - 如何使用 Selenium 和 Python 请求以编程方式识别 ReCaptcha V2 的 32 位数据站点 key 以获取有效响应?

python : Scraping Websites Not Returning any Html

php - 遍历 xpath 中的子 li 节点

xpath:如何不匹配以下数据

python - 我可以使用 k-means 对不完整的图进行聚类吗?

python - 如何为不可变类型实现 "__iadd__()"?

java - 有没有办法判断网络应用程序的编程语言?