【问题标题】:Python Selenium error Element is not reachable by keyboardPython Selenium错误元素无法通过键盘访问
【发布时间】:2021-06-15 01:02:29
【问题描述】:

我已经学习 Selenium 大约一个星期了,在这个项目中,我试图在 Twitter 上制作一个自动推文的机器人。一切都从登录页面解决了,但是当我进入“新推文”选项卡时,我收到了这个错误,说该元素无法通过键盘访问:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"> is not reachable by keyboard

这是 XPATH:

//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div

代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

driver = webdriver.Firefox(executable_path=r"C:\Users\User\Desktop\geckodriver-v0.29.0-win64\geckodriver.exe")
driver.get('https://twitter.com/login')
sleep(3)
box_login = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input').send_keys('***')
box_password = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input').send_keys('***')
box_submit = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span')
box_submit.click()
sleep(5)
# Works until here, where it clicks the new tweet box.

tweet = driver.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "public-DraftStyleDefault-ltr", " " ))]')
tweet.click()
tweet.send_keys('Hello, world. This is my first tweet.')
tweet_submit = driver.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "r-1fneopy", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "r-jwli3a", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "r-qvutc0", " " ))]')
tweet_submit.click()

正如我在评论中所说,它在点击推文框之前一直有效,但在尝试在其上输入内容时出现错误。一直在寻找大约2天的解决方案,希望有人能帮助我!

【问题讨论】:

  • 你需要定位 标签。 (这可能会在设置焦点后显示,或单击...)

标签: python selenium keyboard


【解决方案1】:

发现我要找的课程是class="notranslate public-DraftEditor-content",现在可以正常使用了!

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 1970-01-01
    • 2018-10-08
    • 2019-04-13
    • 1970-01-01
    • 2020-04-29
    • 2019-11-08
    • 2021-10-18
    • 2019-10-13
    相关资源
    最近更新 更多