Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...