简体   繁体   中英

Selenium can't find element in any way

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.

You switched to the iframe, you have to switch back. Try using:

driver.switch_to.default_content()

As @facuew correctly pointed out, once you click on the reCAPTCHA checkbox , to click on the element Anmelden you have to switch to the default content.

Your optimized code block can be:

driver.get('https://wildfall.net/wp-admin/')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#user_login"))).send_keys("username@stackoverflow.com")
driver.find_element(By.CSS_SELECTOR, "input#user_pass").send_keys("simplyGamic")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title=reCAPTCHA]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#wp-submit"))).click()

Browser Snapshot:

野蛮坠落

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM