简体   繁体   中英

Selecting a value from a drop-down option using selenium python

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!

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM