使用Python/Selenium时出现元素不可交互异常

zbdgwd5y  于 2022-10-30  发布在  Python
关注(0)|答案(3)|浏览(246)

我正在尝试登录到一个网站(https://myaccount.play-cricket.com/)使用 selenium 在python。
我正在努力将文本(从python代码中的password变量)发送到网站上的password文本框字段。
下面是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
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

username = 'example@hotmail.com'
password = 'password123'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://myaccount.play-cricket.com/')
time.sleep(5)
driver.find_element(By.NAME, 'email').send_keys(username)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.NAME, 
'password'))).send_keys(password)

错误如下:

mwg9r5ms

mwg9r5ms1#

尝试添加一个等待元素变为可单击的等待,如下所示:

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

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.NAME, "email"))).send_keys(username)
apeeds0o

apeeds0o2#

你的问题与电子邮件验证有关。我发现。
1.如果您先输入密码,则一切正常。
1.如果您离开电子邮件输入字段,那么一切都将工作
因此,一个可能的解决方案是在输入电子邮件后发送enter

username = 'example@hotmail.com'
password = 'password123'
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    driver.get('https://myaccount.play-cricket.com/')
    email_input = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.NAME, "email")))
    email_input.send_keys(username)
    email_input.send_keys(Keys.ENTER)
    password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "password")))
    password_input.send_keys(password)
finally:
    driver.quit()
ezykj2lf

ezykj2lf3#

试试这个
wait.until(错误代码.可单击元素((By.NAME,'password')))
驱动程序.find_element(按.名称,'密码').send_keys(密码)
send_keys(密码). send_keys(密码).send_keys(密码)

相关问题