我需要能够在组合框中选择一个元素,使用 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/