0

This is the code from my Twitter Bot Project.

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")

When I try to run this script, I am getting an 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'

Can someone solve my problem and edit this code?

1
  • What are you trying to achieve on the line the error gets thrown? Commented Aug 27, 2020 at 11:25

3 Answers 3

0

The .send_keys(...) method belongs to the WebElement, not the string.

It's what causes your code to produce this error:

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

Instead of this line:

# 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)

You can change with this code:

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)

You should initialize search_box as WebElement, put the text then submit with enter keys.

Sign up to request clarification or add additional context in comments.

Comments

0

This error message...

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

...implies that you script/program have attempted to invoke send_keys() on a string object.


What went wrong

As per the line of code:

search_text.send_keys(Keys.ENTER)

You are trying to invoke send_keys() on the variable search_text which is of type string passed to def __init__(self,username, password, search_text) method. Where as send_keys() is a method associated with WebElement. Hence you see the error.


Solution

You need to invoke send_keys() on the WebElement as follows:

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)

Refrences

You can find a relevant detailed discussion in:

Comments

0

I don't use twitter so I don't exactly know what search box you are talking about, but If you just want to enter some text in the search box and hit Enter, Then, replace this:

# 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)

with this:

# 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)

I can't test this on my machine as I don't use twitter but I think it should work. Please let me know if this helps. Thanks

2 Comments

Sorry it is not working. I am getting the same error.
Oh sorry! I forgot to modify the code. I've updated the answer. Please check now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.