python - Selenium Web抓取错误: Element Not Interactable

标签 python selenium web-scraping compiler-errors

正如您在下面看到的那样,我正在尝试构建登录APP的小Twitter,但是我一直收到错误消息

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


class TwitterBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.bot = webdriver.Chrome()

    def login(self):
        bot = self.bot
        bot.get("https://twitter.com/login/")
        time.sleep(3)


email = bot.find_element_by_name('session[username_or_email]')
password = bot.find_element_by_name('session[password]')

email.clear()
password.clear()
email.send_keys(self.username)
password.send_keys(self.password)
password.send_keys(keys.RETURN)

输出:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=79.0.3945.88)

最佳答案

首先,您的代码在复制和粘贴时不会产生给定的错误。我必须更正第6和22行以使其正常运行。在此处放置代码时请小心,因为它会阻止答案。

您的问题是session[username_or_email]在Twitter登录页面上不是唯一的元素。该名称实际上有3个元素。 three elements with the target name

您需要选择一个实际上可交互的,它是页面上的第二个session[username_or_email]。与session[password]相同的代码您的代码选择了具有该名称的页面上的第一个元素。您正在搜索具有该名称的页面上的第二个元素。

你必须改变

email = bot.find_element_by_name('session[username_or_email]')
password = bot.find_element_by_name('session[password]')


email = bot.find_elements_by_name('session[username_or_email]')[1]
password = bot.find_elements_by_name('session[password]')[1]

完整工作的代码,并进行了所有修改
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

class TwitterBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.bot = webdriver.Chrome()
    def login(self):
        bot = self.bot
        bot.get("https://twitter.com/login/")
        time.sleep(3)

        email = bot.find_elements_by_name('session[username_or_email]')[1]
        password = bot.find_elements_by_name('session[password]')[1]

        email.clear()
        password.clear()
        email.send_keys(self.username)
        password.send_keys(self.password)
        password.send_keys(Keys.RETURN)

Omar = TwitterBot('Omar Username', 'Omar password')
Omar.login()

关于python - Selenium Web抓取错误: Element Not Interactable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59482897/

相关文章:

string - 无法获取以自定义关键字开头的字符串

python - 开源 Enthought Python 替代方案

python - 为什么我的 app_name 通过导航到 URL 链接显示为复数形式? [登录]

python - 更改anaconda中的默认环境

python ->> 和 << 在 Python 中是什么意思?

java - 如何在没有 'input' 元素的情况下在 Selenium WebDriver 中上传文件

python - Selenium 错误 : UnicodeEncodeError

html - 单击带有 <href> 和 <a> 标签的按钮

python-3.x - python中网络抓取期间的json错误转储

r - 在 R 中使用 download.file 下载时跳过错误文件