AttributeError:“ str”对象在Python中使用Selenium时没有属性“ send_keys”错误

问题描述

这是我的Twitter Bot项目中的代码

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("[email protected]","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'

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

解决方法

我不使用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上测试此功能,因为我不使用Twitter,但我认为它应该可以工作。请让我知道这可不可以帮你。谢谢

,

.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 ,然后输入文字,然后使用Enter键提交。

,

此错误消息...

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

...表示您的脚本/程序已尝试在send_keys()对象上调用string


出了什么问题

按照代码行:

search_text.send_keys(Keys.ENTER)

您正试图对传递给send_keys()方法的类型为 string 的变量search_text调用def __init__(self,username,password,search_text)。其中send_keys()是与WebElement关联的方法。因此,您会看到错误。


解决方案

您需要按以下步骤在 WebElement 上调用send_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)

推荐

您可以在以下位置找到相关的详细讨论: