1

I have been trying for a long time to get Selenium to select an option from a drop down select field. It appears not to be selecting the element whatsoever. Could the fact that it requires a reload link be causing issues?

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import csv
import requests
import re

driver2 = webdriver.Chrome()
driver2.get("http://www.squawka.com/match-results")

el = driver2.find_element_by_id('league-filter-list')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'Football League Championship':
        option.click()
        break

driver2.quit()
2
  • 1
    Can you post the Source for the dropdown element itself? It'll help a lot of finding some other alternatives to select the dropdown.
    – Mercfh
    Commented May 3, 2016 at 17:24
  • 2
    paste your HTML code here so that we can help you.
    – Bharat Mane
    Commented May 4, 2016 at 7:46

3 Answers 3

1

try this:

el = driver2.find_element_by_id('league-filter-list')
el.select_by_visible_text('Football League Championship')
2
  • We prefer answers to have context and not be simply code without explanation.
    – Kate Paulk
    Commented Oct 31, 2016 at 15:01
  • Jose could you elaborate on the difference between this answer and the OP? I think you're correct, but it's taking some heat for being code-only. It's one of those "give a fish, teach to fish" kind of things.
    – corsiKa
    Commented Nov 2, 2016 at 14:59
1

You're trying to click an invisible element, which won't work.

It's easiest to use the select class.

For an example, see Daniel Abel's answer in this thread: What is the correct way to select an <option> using Selenium's Python WebDriver

0

As a workaround for dropdown menus that dont have data before interaction you could simply click text after clicking on element.

findYourDropdown.Click
findSomeText.Click

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.