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.
CodePudding user response:
You likely have an issue with your XPATH selector. Try using an XPATH checker like the Chrome DevTools to check the correct XPATH selector for your element, and make sure the syntax of your XPATH selector is correct.
CodePudding user response:
It mostly happens because the element hasn't loaded completely when you try to click on it. One thing that you can do is to put a time sleep after getting the URL to wait for the page load and then try to find the element like the code below:
driver.get("https://twitter.com/i/flow/login")
time.sleep(3)
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()
CodePudding user response:
There are several problems with your code:
- 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. - You should never use absolute paths as a locators. These locators are extremely breakable. Short unique locators should be used instead.
- 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:\webdrivers\chromedriver.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