AttributeError:“list”对象没有“send\u keys”属性

2024-03-29 07:20:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个Twitter机器人,它可以在我运行脚本时自动登录。但是每当我运行这个脚本时,我都会遇到一个我找不到任何解决方案的错误。有人知道怎么修理吗?在

我试图将element改为elements,将send_keys改为{},但没用

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

    def login(self):
        bot = self.bot
        bot.get('https://twitter.com/')
        time.sleep(3)
        email = bot.find_elements_by_class_name('email-input')
        password = bot.find_elements_by_class_name('session[password]')
        email.clear()
        password.clear()
        email.send_keys(self.username)
        password.send_keys(self.password)
        password.send_keys(Keys.RETURN)

ed = TwitterBot('EMAIL HERE', 'PASSWORD HERE')
ed.login()

我希望它能登录,这样我就可以在我的项目上进一步工作。在


Tags: fromimportself脚本sendemailbotselenium
2条回答

find_elements_xxx将返回元素列表,您不能对列表执行send_keys操作。相反,您必须使用find_element_xxx来返回单个元素,然后才能执行基于元素的操作。在

如果您想获得元素列表,然后对任何特定元素执行操作,那么可以使用下面的逻辑。在

elements = driver.find_elements_by_xxx("locator")
# perform operation on the first matching element
elements[0].send_keys("value_goes_here")
# if you want to perform operation on the last matching element
element[-1].send_keys("value_goes_here")

我现在知道我搞砸了:

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

    def login(self):
        bot = self.bot
        bot.get('https://twitter.com/')
        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)

ed = TwitterBot('EMAIL HERE', 'PASSWORD HERE')
ed.login()

email = bot.find_element_by_name('session[username_or_email]')线上 它是第一个bot.find_element_by_class_name('session[username_or_email]')

感觉很蠢。谢谢你们的帮助!在

相关问题 更多 >