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

这是我的 Twitter 机器人项目的代码。


from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import time



class 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:

            pass



TwitterBot("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'

有人可以解决我的问题并编辑这段代码吗?


撒科打诨
浏览 278回答 3
3回答

MYYA

我不使用 twitter,所以我不完全知道你在说什么搜索框,但如果你只想在搜索框中输入一些文本并按 Enter,那么,将其替换为:# Enter text in the search boxself.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 boxelement = 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,但我认为它应该可以工作。请让我知道这可不可以帮你。谢谢

森栏

该.send_keys(...)方法属于WebElement,而不是字符串。这就是导致您的代码产生此错误的原因:AttributeError:“str”对象没有属性“send_keys”而不是这一行:# Enter text in the search boxself.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,输入文本,然后使用回车键提交。

阿晨1998

这个错误信息...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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python