0

i was trying to build a webscraper but i came to the point where i try to select a select-element() but Selenium cant find the object and throws the Exception.

I used find_element with the correct XPATH (100% sure), but it just doesnt work. I tried to find it by its name but i think the XPATH is the only thing that'll work in my case.

Complete code. The problem is the last selection

import data
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get(data.login_site)

username_input = driver.find_element(By.NAME, "_username")
username_input.send_keys(username)
password_input = driver.find_element(By.NAME, "_password")
password_input.send_keys(password)

login_button = driver.find_element(By.XPATH, "//button[@class='btn btn-primary']")
login_button.click()

plaene_button = driver.find_element(By.XPATH, "//a[@href='/iserv/plan/overview']")
plaene_button.click()

plaene_show = driver.find_element(By.XPATH, "//a[@href='/iserv/plan/show/Plan%20Schueler']")
plaene_show.click()

time.sleep(4)
drpdClass = Select(driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td/form/table/tbody/tr/td[3]/span/nobr/select"))

time.sleep(10)

Picture of the website

2 Answers2

0

maybe element gets rendered after you scrape the page. have you tried delaying your scraping process to see if it works?

hamed danesh
  • 361
  • 9
  • i delayed it by 4 seconds and also tried 10 and the element definitely loads but it doesnt work – Elias Petrat Aug 10 '23 at 19:11
  • I believe something is wrong in your xpath, try getting elements one by one from html down to select to see where it stops. i think you need to have bunch of index in your xpath (like td[0], .etc) – hamed danesh Aug 10 '23 at 19:44
  • i now saw that there is a iframe. I switched to it and tried to select the element, but it doesnt work – Elias Petrat Aug 10 '23 at 20:23
0

Given the html:

html

To identify the select element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    drpdClass = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.selectbox[name='element'][onchange^='doDisplayTimetable']"))))
    
  • Using XPATH:

    drpdClass = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='selectbox' and @name='element'][starts-with(@onchange, 'doDisplayTimetable')]"))))
    
  • Note : You have to add the following imports :

    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.ui import Select
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352