为了账号安全,请及时绑定邮箱和手机立即绑定

AttributeError: 'str' 对象没有属性 'send_keys' 在 Python

AttributeError: 'str' 对象没有属性 'send_keys' 在 Python

撒科打诨 2023-06-27 13:44:53
这是我的 Twitter 机器人项目的代码。from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport timeclass TwitterBot:    def __init__(self,username, password, search_text):        self.driver = webdriver.Chrome()        self.driver.get("https://twitter.com/home?lang=en")        time.sleep(2)        # Enter your username        self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input')\            .send_keys(username)        # Enter your password        self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input') \            .send_keys(password)        self.driver.find_element_by_xpath('/html/body/div/div/div/div[2]/main/div/div/div[1]/form/div/div[3]/div/div')\            .click()        time.sleep(3)        # Enter text in the search box        self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')\            .send_keys(search_text)        search_text.send_keys(Keys.ENTER)        time.sleep(4)        while True:            passTwitterBot("rmail@gmail.com", "abcd1234", "lamborghini")当我尝试运行此脚本时,出现 AttributeError。File "C:\Users\Praneeth Ravuri\PycharmProjects\Twitter Bots\Open Twitter Bots .py", line 24, in __init__    search_text.send_keys(Keys.ENTER)AttributeError: 'str' object has no attribute 'send_keys'有人可以解决我的问题并编辑这段代码吗?
查看完整描述

3 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

我不使用 twitter,所以我不完全知道你在说什么搜索框,但如果你只想在搜索框中输入一些文本并按 Enter,那么,将其替换为:


# Enter text in the search box

self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input').send_keys(search_text)

search_text.send_keys(Keys.ENTER)

有了这个:


# Enter text in the search box

element = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')

element.send_keys(search_text)

element.send_keys(Keys.ENTER)

我无法在我的机器上测试这个,因为我不使用 Twitter,但我认为它应该可以工作。请让我知道这可不可以帮你。谢谢


查看完整回答
反对 回复 2023-06-27
?
森栏

TA贡献1810条经验 获得超5个赞

该.send_keys(...)方法属于WebElement,而不是字符串。


这就是导致您的代码产生此错误的原因:


AttributeError:“str”对象没有属性“send_keys”


而不是这一行:


# Enter text in the search box

self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')\

    .send_keys(search_text)

search_text.send_keys(Keys.ENTER)

您可以使用以下代码进行更改:


search_box = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')

search_box.send_keys(search_text)

search_box.send_keys(Keys.ENTER)

您应该初始化search_box为WebElement,输入文本,然后使用回车键提交。


查看完整回答
反对 回复 2023-06-27
?
阿晨1998

TA贡献2037条经验 获得超6个赞

这个错误信息...


AttributeError: 'str' object has no attribute 'send_keys'

...意味着您的脚本/程序已尝试调用send_keys()对象string。


什么地方出了错

根据代码行:


search_text.send_keys(Keys.ENTER)

您正在尝试调用传递给方法的字符串类型send_keys()变量。其中 as是与WebElement关联的方法。因此你会看到错误。search_textdef __init__(self,username, password, search_text)send_keys()


解决方案

您需要按如下方式调用 WebElementsend_keys():


self.element = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')

self.element.send_keys(search_text)

self.element.send_keys(Keys.ENTER)


查看完整回答
反对 回复 2023-06-27
  • 3 回答
  • 0 关注
  • 263 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信