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()
IAmMilinPatel
  • 9,026
  • 7
  • 41
  • 67
FootyStattoWB
  • 11
  • 1
  • 2

3 Answers3

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

FDM
  • 5,894
  • 1
  • 16
  • 34
1

try this:

el = driver2.find_element_by_id('league-filter-list')
el.select_by_visible_text('Football League Championship')
Jose
  • 191
  • 2
  • We prefer answers to have context and not be simply code without explanation. – Kate Paulk Oct 31 '16 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 Nov 02 '16 at 14:59
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
George
  • 1,412
  • 12
  • 17