【问题标题】:Selenium element is not reachable by keyboard硒元素无法通过键盘访问
【发布时间】:2019-07-18 23:42:48
【问题描述】:

我正在尝试在 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 个元素。

为了确保我做得正确,我在 Amazon 上对其进行了测试,它运行良好,所以不知道为什么 Google 会很困难。

这是我的代码:

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

有什么建议吗?

【问题讨论】:

标签: python selenium selenium-webdriver selenium-firefoxdriver


【解决方案1】:

我把这个放在这里,以防有人自己遇到这个问题。

我无法直接解决登录问题,但我找到了解决方法。

我之前尝试过使用 cookie,但做错了,所以我只是复制了我的 Firefox 配置文件(其中保存了 google 的 auth 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)

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,并做了以下操作:

    1) 我首先使用了“find_element_by_css_selector”函数,它选择具有给定属性的元素的第一次出现。这不起作用。

    2) 然后我使用了“find_elements_by_css_selector”(注意 s),它返回具有给定属性的元素列表。该列表中有 2 个元素。当然,第一个(索引 [0])不能通过键盘访问:这相当于上面的 (1)。但是第二个元素(带有索引 [1])可以通过键盘访问。 问题解决了。

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2021-06-15
      • 2018-10-08
      • 1970-01-01
      • 2019-11-08
      • 2016-08-09
      • 2016-10-14
      • 2012-01-09
      • 1970-01-01
      相关资源
      最近更新 更多