How to click on next in the twitter sign in page using selenium – Python

Question:

I am trying to create a twitter automation bot using selenium but when I am trying to log in and click on the next button after entering the userID I am getting an error.

I have tried using xpath, link_text, css_selector but still keep getting the same error


from bs4 import BeautifulSoup
import requests
import random
import datetime
from datetime import timedelta
import time
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import schedule
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 20)

driver.get("https://twitter.com/i/flow/login")

element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, '[name="text"]')))
element.send_keys("[email protected]")

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='css-901oao r-1awozwy r-jwli3a r-6koalj r-18u37iz r-16y2uox r-37j5jr r-a023e6 r-b88u0q r-1777fci r-rjixqe r-bcqeeo r-q4m81j r-qvutc0'"))).click()


I am getting the following error no matter what I try:

Traceback (most recent call last):
  File "main.py", line 28, in <module>
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='css-901oao r-1awozwy r-jwli3a r-6koalj r-18u37iz r-16y2uox r-37j5jr r-a023e6 r-b88u0q r-1777fci r-rjixqe r-bcqeeo r-q4m81j r-qvutc0'"))).click()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
#0 0x55aea1405919 <unknown>
Asked By: rzee1991

||

Answers:

The "Next" button can be located by the following XPath: "//div[@role='button'][contains(.,'Next')]".
The simplest nad clearest way to locate that element is by role='button' attribute and Next text content in it’s child element. Locating element by text supported by XPath only this is why I used XPath here.
This code works:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

url = "https://twitter.com/i/flow/login"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[name="text"]'))).send_keys("[email protected]")
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@role='button'][contains(.,'Next')]"))).click()

The result is

enter image description here

Answered By: Prophet