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("rmail@gmail.com", "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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

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

Devansh Soni
  • 771
  • 5
  • 16
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.

frianH
  • 7,295
  • 6
  • 20
  • 45
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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352