I am trying to fill out a login form with python code. For that I need to click submit as the last step.
But Selenium can't find the submit button, wether by ID, Class, CSS Selector or Name.
Here is my full code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
website_link = "https://wildfall.net/wp-admin/"
username = "xxxx"
password = "xxxx"
element_for_username = "user_login"
element_for_password = "user_pass"
element_for_submit = "wp-submit"
browser = webdriver.Chrome()
browser.get((website_link))
time.sleep(5)
username_element = browser.find_element(By.ID, element_for_username)
username_element.send_keys(username)
password_element = browser.find_element(By.ID, element_for_password)
password_element.send_keys(password)
WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox']/div[@class='recaptcha-checkbox-border']"))).click()
time.sleep(10)
submit_element = browser.find_element(By.ID, element_for_submit)
submit_element.click()
Part where the error occurs
submit_element = browser.find_element(By.ID, element_for_submit)
submit_element.click()
Here is the error
>>> submit_element = browser.find_element(By.ID, element_for_submit)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="wp-submit"]"}
(Session info: chrome=93.0.4577.63)
I don't understand why it can't find the button, as it finds all others. It's also no iframe so can't be because of that. Maybe I just don't find the correct Name/Class/ID for the Button but there's not much to choose from, so I'm lost at this point.
CodePudding user response:
You switched to the iframe, you have to switch back. Try using:
driver.switch_to.default_content()
CodePudding user response: