26

I want to select a value from a drop-down option. The html is as follows:

<span id="searchTypeFormElementsStd">

    <label for="numReturnSelect"></label>
    <select id="numReturnSelect" name="numReturnSelect">
        <option value="200"></option>
        <option value="250"></option>
        <option value="500"></option>
        <option selected="" value="200"></option>
        <option value="800"></option>
        <option value="15000"></option>
        <option value="85000"></option>
    </select>

</span

I tried as follows:

find_element_by_xpath("//select[@name='numReturnSelect']/option[text()='15000']").click()

What is wrong with it? Please help me!

3
  • Perhaps you Need @value, not text()? My xpath-foo is too weak to be sure. Commented Mar 20, 2014 at 6:02
  • Not sure in Python. But hope you have a Select Module as similar to Select class in Java Commented Mar 20, 2014 at 6:05
  • The following error occurred:\nInvalidSelectorError Commented Mar 20, 2014 at 6:13

2 Answers 2

45

Adrian Ratnapala is right and also i would choose id over name, so you can try the following :

find_element_by_xpath("//select[@id='numReturnSelect']/option[@value='15000']").click()

OR

find_element_by_css_selector("select#numReturnSelect > option[value='15000']").click()

OR

you can use select_by_value(value) :

Select(driver.find_element_by_css_selector("select#numReturnSelect")).select_by_value(15000).click()

Click here for more info on Select.

Sign up to request clarification or add additional context in comments.

4 Comments

The first one worked, but I did not tried second one. Thanks! BTW what magic you did?
Hahaha its no magic, option[text()='15000'] will check if any option tag has text 15000 but in your case 15000 was value attrubute. Anyways glad it Helped! i suggest you play around with the second and third as well, will help you explore selenium.
I hope you can answer my new question as well: stackoverflow.com/questions/22525567/…
Its a webpage, just lookup by CSS
5
from selenium.webdriver.support.ui import Select
driver = webdriver.Ie(".\\IEDriverServer.exe")
driver.get("https://test.com")
select = Select(driver.find_element_by_xpath("""//input[@name='n_name']"""))
select.select_by_index(2)
select.select_by_visible_text('Visible Text')
select.select_by_value('value')

It will work fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.