繁体   English   中英

Selenium 无法以任何方式找到元素

[英]Selenium can't find element in any way

我正在尝试使用 python 代码填写登录表单。 为此,我需要单击提交作为最后一步。

但是 Selenium 找不到提交按钮,无论是通过 ID、类、CSS 选择器还是名称。

这是我的完整代码

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()

发生错误的部分

submit_element = browser.find_element(By.ID, element_for_submit)
submit_element.click()

这是错误

>>> 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)

我不明白为什么它找不到按钮,因为它找到了所有其他按钮。 它也不是 iframe,所以不能因此。 也许我只是没有找到正确的按钮名称/类/ID,但没有太多可供选择,所以我现在迷路了。

你切换到 iframe,你必须切换回来。 尝试使用:

driver.switch_to.default_content()

正如@facuew正确指出的那样,单击 reCAPTCHA 复选框后,要单击元素Anmelden ,您必须切换到默认内容。

您优化的代码块可以是:

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()

浏览器快照:

野蛮坠落

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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