Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Splinter. How to select dropdown list which doesn't have name in option section

I try to select "Local Host" from the dropdown list which has the following html code:

<select id="server_select">
    <option></option>
    <option>
        Local Host
    </option>

    <option>
        ah005
    </option>

</select>

Here is my python code to use splinter module to select "Local Host" from dropdown list but failed.

server_list = browser.find_by_xpath('//select[@id="server_select"]//option[@selected="Local Host"]').first
server_list.click()

and I got the error:

 Traceback (most recent call last):
  File "splinter_test.py", line 22, in <module>
    server_list = browser.find_by_xpath('//select[@id="server_select"]//option[@selected="Local Host"]').first
  File "/Users/Bibo_MBPR/anaconda/lib/python2.7/site-packages/splinter/element_list.py", line 53, in first
    return self[0]
  File "/Users/Bibo_MBPR/anaconda/lib/python2.7/site-packages/splinter/element_list.py", line 44, in __getitem__
    self.find_by, self.query))
splinter.exceptions.ElementDoesNotExist: no elements could be found with xpath "//select[@id="server_select"]//option[@selected="Local Host"]"

Could someone please give me some advice? Thanks

like image 264
weiting chen Avatar asked Feb 15 '26 12:02

weiting chen


1 Answers

You can use find_by_id to get the browser element for drop down as it has the id="server_select" option. Then you can select your option from them by text. Try the following-

server_list = browser.find_by_id('server_select')
server_list.select_by_text("Local Host")

Or,

browser.find_option_by_text("Local Host").first.click()
like image 159
Nawshad Rehan Rasha Avatar answered Feb 18 '26 03:02

Nawshad Rehan Rasha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!