Python Selenium NoSuchElementException although the element is there

52 Views Asked by At

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

There are 2 best solutions below

3
hamed danesh On

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

0
undetected Selenium On

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