正如您在下面看到的那样,我正在尝试构建登录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个元素。
您需要选择一个实际上可交互的,它是页面上的第二个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/