ChrisG29
ChrisG29

Reputation: 1571

Trouble with identifying a value in a drop down (Selenium Python)

I'm using Selenium (Python) to select a value from a drop down list, but I'm having some problems.

When I use Selenium IDE, it identifies the drop down as a button, as follows :

    driver.find_element_by_xpath("(//button[@type='button'])[2]")

I then tried to select a value from the list by using:

    Select(driver.find_element_by_xpath("(//button[@type='button'])[2]")).select_by_visible_text("Africa/Juba")

That doesn't work because it says "Select only works on elements, not on buttons.

When I inspect the element, I can't see anything useful that I can use to identify the drop down (this is just some of it):

<select selectpicker="" ng-options="timezone for timezone in model.timezones" ng-model="newAccount.timeZoneId" class="form-control ng-pristine ng-untouched ng-valid bs-select-hidden" data-live-search="true">  <option value="0" label="Africa/Abidjan">Africa/Abidjan</option><option value="1" label="Africa/Accra">Africa/Accra</option>  <option value="2" label="Africa/Addis_Ababa">Africa/Addis_Ababa</option>  <option value="3</option></select>  

Is there another way that I can select a value from the drop down list?

Upvotes: 1

Views: 281

Answers (1)

RemcoW
RemcoW

Reputation: 4326

The element you are getting with your Xpath is a button. You cannot use Select with buttons. You want to get the select element. Try this:

Select(driver.find_element_by_xpath("//select[@ng-model='newAccount.timeZoneId']")).select_by_visible_text("Africa/Juba")

Edit The problem you are having makes sense. In the HTML you shared there is no option element containing the text Africa/Juba. If you would change Africa/Juba to Africa/Algiers it should work without problem. In the comments you mentioned this this element: <span class="text">Africa/Juba</span>. This is missing from your HTML example. However a span inside a select should not work. You can use the Select class when dealing with options.

Upvotes: 2

Related Questions