-1

I'm just trying to scrape a website, but i got stuck. Here is my code:

from selenium import webdriver

driver = webdriver.Firefox(executable_path='location of the geckodriver')
driver.get('http://kernyilvantartas.zalajaras.hu/public/')
driver.find_element_by_xpath("""//*[@id="btn"]""").click()
# the dropdown menu
driver.find_element_by_xpath("""//*[@id="lap"]""").click()
# click on page 2
driver.find_element_by_xpath("""//*[@id="lap"]/option[2]""").click()

After the click() it only highlights the 2nd option on the dropdown menu but doesnt jump to the next page. Any idea?

Guy
  • 46,488
  • 10
  • 44
  • 88

1 Answers1

0

The element you are trying to interact is a <select> node, so you need to use the Select class and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and select_by_value() method:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.select import Select
    
    driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get('http://kernyilvantartas.zalajaras.hu/public/')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#btn"))).click()
    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[name='lap']"))))
    select.select_by_value("2")
    
  • Using XPATH and select_by_visible_text() method:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.select import Select
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='btn']"))).click()
    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='lap']"))))
    select.select_by_visible_text("2")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have tried both way , with Firefox and Chromedriver too but the results were as earlier. Is this work on your side? – kntrdzs Jan 15 '20 at 14:40
  • :( I have been afraid of that. However, if you use Firefox webdriver, the manual way works fine, but the programatically doesn't. Thx for your quick help! – kntrdzs Jan 15 '20 at 14:53