2014-11-24 93 views
1

我需要能够在组合框中选择一个元素,使用xpath似乎是做到这一点的方法。我找到了路并且是使用硒webdriver选择组合框xpath

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> 
+0

你能显示包含元素的HTML,或更好的分享链接的网站,如果可能的话。谢谢。 – alecxe 2014-11-24 15:00:20

+0

好的,我加了什么, – 2014-11-24 15:03:30

回答

4

硒具有处理select一种特殊的方式 - >option元素 - 一个Select class。寻找你的select标签由ID,实例化一个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") 
+0

我遇到的一个问题是我收到错误,无法读取属性'点击'空。你认为可能会导致这个错误 – 2014-11-24 16:33:30

+0

这个链接有帮助吗,@aaronbill? [http://stackoverflow.com/questions/24043355/typeerror-cannot-read-property-click-of-null](http://stackoverflow.com/questions/24043355/typeerror-cannot-read-property-click-的空) – Subh 2014-11-24 17:00:58