8

I'm using Selenium in Python to open a web page and I'm trying to get the list of values from a specific dropdown list. Let's say the HTML code for the dropdown list looks like this:

<select class="mdc-select__input" name="nouveau-num" data-msisdn-loaded="0">                           <option value="" selected="selected"></option>
                     <option value="351 8320175">351 8320175</option>
<option value="351 8652736">351 8652736</option>
<option value="351 8783295">351 8783295</option>
<option value="351 8094085">351 8094085</option>
<option value="351 8861691">351 8861691</option>
<option value="351 8271705">351 8271705</option>
<option value="351 8970191">351 8970191</option>
<option value="351 8965848">351 8965848</option>
<option value="351 8353924">351 8353924</option>
<option value="351 8988158">351 8988158</option>
</select>

And I want to retrieve all the values between <option> tags. I tried to do a browser.page_source which returns the HTML source of the web page and then do a regular expression (something like <option value="[0-9 ]*">) but the result is empty. For some reason however, the HTML code above is not in the HTML page source code retrieved by Selenium. Any ideas how I can approach this differently/what is wrong with the current approach?

1
  • If you ready to use Ruby, then this would be one line of code puts b.select_list(name: "nouveau-num").options.map{|option| option.text} Commented Jun 23, 2018 at 17:00

4 Answers 4

6

You can create a Select object and iterate over the amount of options with a loop.

For example:

from selenium.webdriver.support.ui import Select
selector = Select(driver.find_element_by_name("nouveau-num"))
options = selector.options
for index in range(0, len(options)-1):
    print(options[index])

Edit:

I tried the code on the link you provided and there seems to be a delay until the dropdown's values are loaded. In addition I forgot that options has a list of elements so you need to specify .text. On top of all that By.NAME seems to work better than find_element_by_name

Here is the corrected code:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

dropdown = driver.find_element(By.NAME, "nouveau-num")

selector = Select(dropdown)

# Waiting for the values to load
element = WebDriverWait(driver, 
10).until(EC.element_to_be_selected(selector.options[0]))

options = selector.options
for index in range(1, len(options)-1):
    print(options[index].text)

Using this code I receive the following results:

351 8631174
351 8586821
351 8014561
351 8831839
351 8957001
351 8673968
351 8612034
351 8585995
351 8438130
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but for some reason it's not working. The website I'm using is registrazione.iliad.it and I first click the "no" radio button, after which the dropdown menu appears from which I want to extract the values.
2

According to this brilliant answer parsing HTML with Regex is never a good idea.

You better use find_elements_by_css_selector or find_elements_by_xpath.

Example with css selectors:

for tag in browser.find_elements_by_css_selector('select[name=nouveau-num] option'):
    value = tag.get_attribute('value')
    text = tag.text

1 Comment

I tried this but for some reason it's not working. The website I'm using is registrazione.iliad.it and I first click the "no" radio button, after which the dropdown menu appears from which I want to extract the values.
0

for this i do:

  1. Get the xpath. (//label/div/div[1]/div[1]/div[1])

  2. Put "/*" at the end (//label/div/div[1]/div[1]/div[1]/*)

  3. Find it with driver.find_elements (This is important, if you ommit the "s" of the end it will fail) (lista = driver.find_elements(By.XPATH, '//label/div/div[1]/div[1]/div[1]/*'))

  4. then, you will get a list of web elements (not of strings), i called it "lista"

  5. use a for loop and put the values into a list.

    listaItems = list()

    for i in listaItems: listaItems.appeend(i.text)

And thats all.

Comments

0
def verify_dropdown_value(self, Elementlocator, LocatorType, Expectedvalue):
    time.sleep(5)
    Value = self.helper.identify_element(Elementlocator, LocatorType, "Value")
    #ActualValue = Value.get_attribute('value')

    #options = Value.options
    ActualValue = Value.text

    if ActualValue == Expectedvalue:
        print("Pass")
        return True
    else:
        print("Fail")
        return False

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.