0

I want to selectively iterate through a drop down menu in this case starting from 2010 to the present year (I want this to be open ended as I need to run it in the future).

I have only come across previous questions where either one selection or every selection in the drop down is wanted.

Chrome_Path = r"C:\Users\George\Desktop\chromedriver.exe"

driver.get("http://www.snookerdatabase.co.uk")

driver.find_element_by_xpath("""//*[@id="Table1"]/tbody/tr[3]/td[1]/b/a[2]""").click()

driver.find_element_by_name("year").click()
Louis Saglio
  • 1,120
  • 10
  • 20
Snoopy94
  • 15
  • 5
  • Possible duplicate of [Selenium (Python) - SELECT](https://stackoverflow.com/questions/32382415/selenium-python-select) – JeffC Mar 28 '19 at 20:37
  • The selected answer will answer your question, just remove the extra `select = ...` line if your `SELECT` doesn't refresh the page on selecting an option. – JeffC Mar 28 '19 at 20:38

2 Answers2

2

Use Select to select the value from dropdown.

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

driver = webdriver.Chrome()


driver.get("http://www.snookerdatabase.co.uk")

wait=WebDriverWait(driver,5)
wait.until(expected_conditions.element_to_be_clickable((By.XPATH,'//*[@id="Table1"]/tbody/tr[3]/td[1]/b/a[2]'))).click()

i=2010
while i<2020:
 select = Select(driver.find_element_by_name("year"))
 select.select_by_value(str(i))
 i=i+1
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Why `driver.get("https://www.youtube.com/watch?v=l5LfjYmNEJs&t=160s")` in between :) – undetected Selenium Mar 28 '19 at 16:43
  • Thanks for the answer, how would you go about grabbing stuff as it iterates through each year?I'm looking to enter into specific links for each year and scrape data from inside the links. – Snoopy94 Mar 28 '19 at 16:49
  • 1
    @DebanjanB : Thanks i have removed that:) – KunduK Mar 28 '19 at 17:40
  • @Snoopy94 : take the driver.page_source and use beautiful soup to scrap this. – KunduK Mar 28 '19 at 17:43
  • I have tried this but I think the problem is changing a selection in the drop down doesn't alter the page_source (URL unchanged) and therefore beautiful soup just scrapes the 2019 links over and over – Snoopy94 Mar 28 '19 at 19:02
  • Can you post separate question and mentioned what your expected output? – KunduK Mar 28 '19 at 19:46
0

That should help you:

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('name'))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)
irezwi
  • 789
  • 8
  • 14