我正在尝试使用 python 中的 selenium 登录我的 Google 帐户。我不断遇到以下错误:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <input class="whsOnd zHQkBf" name="password" type="password"> is not reachable by keyboard
我看过其他一些类似的问题,但其中大多数都与身份验证失败有关。另post建议寻找一些覆盖它的东西,但我什么也没看到。此外,输入框不会根据其 html 隐藏:
<input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" dir="ltr" data-initial-dir="ltr" data-initial-value="" badinput="false">
我还尝试在访问密码输入框之前放置一些等待语句,但没有成功。 我还检查以确保 find_elemnt_by 函数仅返回 1 个元素。
为了确保我做得正确,我在亚马逊上进行了测试,它运行得很好,所以不知道为什么谷歌的很难。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# create a new Firefox session
driver = webdriver.Firefox(executable_path="D:\Selenium\geckodriver.exe")
driver.implicitly_wait(5)
# Navigate to youtube
driver.get("https://www.youtube.com/")
sign_in_button = driver.find_element_by_css_selector('ytd-button-renderer.style-scope:nth-child(5)')
sign_in_button.click()
username = driver.find_element_by_css_selector('#identifierId')
username.send_keys('johndoe@gmail.com')
next_button = driver.find_element_by_css_selector('#identifierNext')
next_button.click()
password_text_box = driver.find_element_by_css_selector('.I0VJ4d > div:nth-child(1) > input:nth-child(1)')
# FAILS HERE
password_text_box.send_keys('fakepassword')
next_password_button = driver.find_element_by_css_selector('#passwordNext')
next_password_button.click()
有什么建议吗?
最佳答案
我将其放在这里,以防有人自己遇到这个问题。
我无法直接解决登录问题,但我找到了解决方法。
我之前曾尝试使用 cookie,但做错了什么,因此,我只是复制了我的 Firefox 配置文件(其中保存了 google 的身份验证 cookie)并将其附加到网络驱动程序。
ffp = FirefoxProfile(r"C:\Users\user\AppData\Roaming\Mozilla\Firefox\Profiles\sel_test")
driver = webdriver.Firefox(executable_path="D:\Selenium\geckodriver.exe", firefox_profile=ffp)
关于python - 键盘无法访问 Selenium 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103889/