使用 selenium webdriver xpath 选择组合框

aar*_*ill 2 python selenium xpath webdriver selenium-webdriver

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

Path = div/div/div
ID = stuff_devicessettings_Modbus_TCP
Run Code Online (Sandbox Code Playgroud)

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

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"])
Run Code Online (Sandbox Code Playgroud)

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

这是包含的内容

<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>
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 5

Selenium 有一种特殊的处理方式select->option元素 - 一个Selectselect通过 id找到您的标签,实例化一个Select类并通过可见文本选择一个选项

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")
Run Code Online (Sandbox Code Playgroud)