Skip to content
Advertisement

How to solve “selenium.common.exceptions.NoSuchElementExceptio” in selenium when creating a twitter bot?

I am trying to creating a twitter bot and I am stuck on the log in page.
Here is my code:

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

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


driver = webdriver.Chrome(options=chrome_options)

driver.get("https://twitter.com/i/flow/login")
# for twitter
driver.find_element(By.XPATH,'/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]').click()

I am always getting the the below mentioned error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]"}
  (Session info: chrome=91.0.4472.101)
Stacktrace:
#0 0x55f817605919 <unknown>

I have tried it with css_selector, xpath, class, ID but nothing seems to work. I am not able to click the box to enter my email ID and password.

Can some one please show me how to solve this error.

Advertisement

Answer

There are several problems with your code:

  1. You need to wait for element to be clickable before accessing it. The best way to do that is to use WebDriverWait expected_conditions explicit waits.
  2. You should never use absolute paths as a locators. These locators are extremely breakable. Short unique locators should be used instead.
  3. The element you trying to click is not the element should be clicked there.

The following code works:

from selenium import webdriver
from selenium.webdriver import ActionChains
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(service=webdriver_service, options=options)

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

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

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[autocomplete='username']"))).click()

The result is

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement