4

I've made a small code whose purpose is to log me into a website and among select an option in a dropdown menu among other things. And I am unable to figure out how to do this.

I'm using selenium with python and things are working excellent except this piece of code about the dropdown menu:

# dropdown  
element = browser.find_element_by_id("rating")
for option in element.find_elements_by_tag_name("option"):
    if option.text == "It's OK":
        option.click()
        break

This is the html code of the page regarding the dropdown menu:

<select name="rating" id="rating" size="1" style="margin-bottom:6px;">
    <option value=""></option>
    <option value="5">I Love it!</option>
    <option value="4">I Like it</option>
    <option value="3">It's OK</option>
    <option value="2">I Don't like it</option>
    <option value="1">I Hate it!</option>
</select>

With this code no error is displayed just it doesn't select anything.

I've also tried the Select function with:

find_element_by_css_selector("select#rating > option[value='2']").click()

But this throwing this error:

NameError: name 'find_element_by_css_selector' is not defined
Backup
  • 66
  • 1
  • 8

3 Answers3

7

For select tag you need to use below approach to select a option

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('rating'))
select.select_by_index("3")
// or
select.select_by_visible_text("It's OK")
// or
select.select_by_value("3")

Let me know if having any issue

  • Thanks for your reply, unfortunately this won't solve the problem. There is no error actually, but in the browser nothing is selected (the "It's OK" is not selected). – Backup May 30 '17 at 13:05
2

I setup a quick page to test this and it worked!!!

Here is the updated code.

 #!/usr/bin/env python3
 from selenium import webdriver

 browser = webdriver.Firefox()

 site = browser.get('http://localhost:8000/')

 element = browser.find_element_by_id("rating")

 for option in element.find_elements_by_tag_name("option"):
     print(option.text)
     if option.text == "It's OK":
        option.click()
        print('fount it!!!')
        break




I Love it!
I Like it
It's OK
fount it!!!

My output.

  • I'm on Python 2.7 and I actually don't know why this is not working. Now I will try the second link you provided. The only thing I can think about it that the drivers of the browser are old (or something, is it possible?!). – Backup May 30 '17 at 14:15
  • Use python 3 then, I had no problems with it – Bernard 'Beta Berlin' Parah May 30 '17 at 14:27
  • Should I have to uninstall the 2.7 version first? My only concern is about all the dependencies I have for the 2.7 which won't work on 3+ – Backup May 30 '17 at 14:36
  • it depends, I don't know how large the project is and how important the dependencies are to the project, you can use both versions of python on the same machine. You just need to specify the version to use for a particular script at the top. ```#!/usr/bin/env python3``` , once I take this off, my project will default to python 2. – Bernard 'Beta Berlin' Parah May 30 '17 at 14:39
  • Ok my bad. I'm on linux and found out that I already have Python3. So I'll add this string at the beginning and change accordingly the code. Will update on the output! @Bernard Parah – Backup May 30 '17 at 14:49
0
 Please try the following and let me know. This will click the parent element  first, so that, list of values will be displayed on the page and then click on the options.
element= browser.find_element_by_id("rating")
element.click()
for option in element.find_elements_by_tag_name("option"):
    if option.text == "It's OK":
        option.click()
        break
Murthi
  • 5,299
  • 1
  • 10
  • 15
  • same thing as mi initial issue: no error provided, but seems like bypassing those lines, since nothing is selected in the browser. – Backup May 30 '17 at 14:35