2014-11-24 98 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