Python Selenium Can't Find Any Element On A Webpage
I want to automate a simple task with selenium. Login to this website: https://www.lernsax.de/. I'am trying to locate the element via xpath but that doesn't work at all and I get a
Solution 1:
An iframe is present on the page, so you need to first switch the driver to the iframe and the operate on the element:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://lernsax.de")
# Switch to iframe
driver.switch_to.frame(driver.find_element_by_id('main_frame'))
# Find the element by applying explicit wait on it and then click on it
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='skeleton_main']/div[1]/div[2]/div/a"))).click()
Solution 2:
Looks like the login form is contained within an iframe, which you will need to switch into, and perform the operations you need. Add below before you click on login button.
driver.switch_to.frame('main_frame')
Solution 3:
As the the desired element is within an <iframe>
so to invoke click()
on the element you have to:
- Induce WebDriverWait for the desired
frame_to_be_available_and_switch_to_it()
. - Induce WebDriverWait for the desired
element_to_be_clickable()
. You can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:driver.get('https://www.lernsax.de/wws/9.php#/wws/101505.php?sid=97608267430324706358471707170230S5c89c6aa') WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#content-frame"))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-qa='reporting-filter-trigger-toggle'][data-ember-action]"))).click()
Using
XPATH
:driver.get('https://www.lernsax.de/wws/9.php#/wws/101505.php?sid=97608267430324706358471707170230S5c89c6aa') WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='main_frame']"))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='mo' and text()='Login']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC
Browser Snapshot:
Reference
You can find a coupple of relevant discussions in:
Post a Comment for "Python Selenium Can't Find Any Element On A Webpage"