【问题标题】:python selenium send_key() is not workingpython selenium send_key() 不工作
【发布时间】:2019-12-24 20:03:29
【问题描述】:

我是自动化的新手。我正在尝试在 Omegle 的文本框中输入一个字符串,然后按 Enter,但它向我显示了一个错误:

selenium.common.exceptions.ElementNotInteractableException:消息:键盘无法访问元素

here 是我要访问文本框的网页链接。

import time
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

driver.get("https://www.omegle.com/")
time.sleep(random.randint(2, 5))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(random.randint(1, 5))
button = driver.find_element(By.XPATH, "//span[@class='topicplaceholder']")
button.click()
button.send_keys("panda")
button.send_keys(Keys.RETURN)

【问题讨论】:

  • 如果您单击的是一个按钮,那么您应该无法向它发送密钥,因为它不是文本输入点。重新评估网站以获得更好的互动方式
  • 我怎样才能找到输入点,在项目中我又遇到了同样的问题,但在不同的文本字段,你能告诉我如何找到可交互的点吗?

标签: python python-3.x selenium automation


【解决方案1】:

我试过了,效果很好。

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

browser = webdriver.Chrome()
browser.get("https://www.omegle.com/")
text_area = browser.find_element_by_class_name("topicplaceholder")
text_area.click()
input = browser.find_element_by_class_name("newtopicinput")
input.send_keys("panda")
input.send_keys(Keys.RETURN)

我希望已经解决了你的问题。

【讨论】:

    【解决方案2】:

    应该工作:

    button = driver.find_element_by_xpath('/html/body/div[3]/table/tbody/tr[2]/td[1]/div/div/div[1]/span[2]')
    button.click()
    button.send_keys('panda')
    button.send_keys(u'\ue007')
    

    【讨论】:

    • 它抛出错误 selenium.common.exceptions.ElementNotInteractableException: 消息:元素 无法通过键盘访问,谢谢您的帮助
    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2015-12-06
    • 2012-01-02
    • 2017-05-13
    相关资源
    最近更新 更多