aaron bill
aaron bill

Reputation: 55

Selecting a combo box using selenium webdriver xpath

I need to be able to select a element in a combo box, using xpath seems to be the way to do it. i found the path and the id of the box which is

Path = div/div/div
ID = stuff_devicessettings_Modbus_TCP

I tried several different ways to open the box here is some of my failed attempts

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"])

But none of it was successful, Please help me find out a way to open and select an element from a combo box.

Here is what is contained

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

Upvotes: 2

Views: 2813

Answers (1)

alecxe
alecxe

Reputation: 473943

Selenium has a special way of handling select->option elements - a Select class. Find your select tag by id, instantiate a Select class and 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")

Upvotes: 5

Related Questions