Home > Blockchain >  Trying to Tweet without API using Python, Selenium and Chrome Web Driver
Trying to Tweet without API using Python, Selenium and Chrome Web Driver

Time:02-04

With the recent announcement of Twitter charging for their API usage, I am trying to Tweet without it. But I am running into issues, from my Ubuntu box. Below is combination of various Python scripts found online.

The error I am running into is after successful login, I get

selenium.common.exceptions.ElementClickInterceptedException:
Message: element click intercepted: Element <div dir="ltr" 
>...</div> is not clickable at point (0, 1).
Other element would receive the click:
<header role="banner" >...</header>

Code

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
options.add_argument(f'user-agent={userAgent}')
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

url = 'https://twitter.com/login'

# change this to your username and password
userUsername = 'user'
userPassword = 'pw'

def main():    
    browser.get(url)
    
    browser.implicitly_wait(5)

    user = browser.find_element("name", "text")
    user.send_keys(userUsername)
    user.send_keys(Keys.ENTER)
    
    browser.implicitly_wait(2)
    
    password = browser.find_element("name", "password")
    password.send_keys(userPassword)
    browser.implicitly_wait(5)
    
    password.send_keys(Keys.ENTER)
    
    browser.implicitly_wait(0)
    
    button1 = browser.find_element(By.CSS_SELECTOR,'div[dir="ltr"]')
    button1.click()
    browser.implicitly_wait(5)  
    
    tweet = browser.find_element(By.CSS_SELECTOR,"br[data-text='true']")
    tweet.click()
    
    browser.implicitly_wait(1)
    tweet.send_keys('Testing1234')

    button2 = browser.find_element(By.CSS_SELECTOR,"div[data-testid='tweetButtonInline']")
    
    button2.click()

    browser.close() 
main()

CodePudding user response:

Instead of this locator, button1 = browser.find_element(By.CSS_SELECTOR,'div[dir="ltr"]') to click on post a tweet use below locator.

button1 = browser.find_element(By.CSS_SELECTOR,'a[aria-label="Tweet"]')

Please provide delay or explicit wait while interacting with the element.


Also need to change tweet message locator and Tweet button

tweet = browser.find_element(By.CSS_SELECTOR,'div[aria-label="Tweet text"]')

AND

button2 = browser.find_element(By.CSS_SELECTOR,"div[data-testid='tweetButton']")
  • Related