Selenium can't find element | Python

Viewed 33

So I want to read the text on a Website (extracting the data). But when i try to find the element, python prints out an error message (no such element exception...). The element does not have a unique id etc. so i use the "XPATH", which does not work. In this case i want to get the element with the text(or data) "11 1. Jahrgangsstufe".enter image description here When I right click and copy the "XPATH", i copy this: "/html/body/center2/p[11/table/tbody/tr[61]/td".But as said this does not work as XPATH. Does someone has any clues ? Are there maybe elements selenium can't access? My Code so far: enter image description here

Code as text:

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from secrets import  url
path = 'C:\Program Files (x86)\chromedriver.exe'

# opening chrome and website
driver = webdriver.Chrome(path)
driver.get(url)
sleep(0.1)

# clicking on right timetable
timetable = driver.find_element(By.CLASS_NAME, 'timetable-wrapper')
timetable.click()
sleep(0.1)

# opening the page on a clearer view (hardly to explain),
# but I did this because the "XPath helper"
# Chrome extension tool worked better here.
# also opens a new tab (if this is relevant)
other_page = driver.find_element(By.XPATH,"/html[@class=' js flexbox flexboxlegacy canvas canvastext webgl no-touch geolocation postmessage websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers no-applicationcache svg inlinesvg smil svgclippaths']/body[@class='noTouch noScroll']/div[@id='overlay']/div[@class='overlay-element']/div[@class='overlay-head']/div[@class='zoom-controls']")
other_page.click()
sleep(0.5)
# Here I want to get the right box, that has the XPAHT :
# /html/body/center[1]/p[1]/table/tbody/tr[61]/td
# But it cant be found. When I try to print out the page source, not the #complete page source is printed out too.

1 Answers

From your explanation, I believe you need to wait for that element to load. First, make sure you have the following imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

After that, try to locate the element like so:

trouble_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "--your-actual-xpath--")))
print(trouble_element.get_attribute('outerHTML')

And it should print out the actual element's HTML, if the XPATH you use is correct.

Selenium documentation is quite extensive: https://www.selenium.dev/documentation/

Related