0

I am writing a script in python that logs in twitter however whenever i try to locate the log-in button in selenium it gives an error

Python code:

driver= webdriver.Firefox()
driver.get("https://twitter.com")
login_button= driver.find_element(By.XPATH, "//a[@href='/i/flow/signup']")         print(login_button)

Source of the target element:

Source of the login button element:

The error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //a[@href='/i/flow/signup']

I have even tried copying the absolute path of the element:

driver= webdriver.Firefox()
driver.get("https://twitter.com")
login_button= driver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/main/div/div/div[1]/div[1]/div/div[3]/a")
print(login_button)

This gives the error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/div/div/div\[2\]/main/div/div/div\[1\]/div\[1\]/div/div\[3\]/a
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

As error says: Unable to locate element. You can use wait to "wait" until your element is located. For more you can see: https://selenium-python.readthedocs.io/waits.html So you can do that as following code:

#import necessary parts
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#wait
wait = WebDriverWait(driver, 10)

#from your code
driver= webdriver.Firefox()
driver.get("https://twitter.com")
login_button = wait.until(EC.presence_of_element_located((By.XPATH, "//a[@href='/i/flow/signup']"))).click() #it will click the button.
Furkan Ozalp
  • 334
  • 1
  • 4
  • 7
0

To click on Sign in button within Twitter Login Page you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://twitter.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login'] span > span"))).click()
    
  • Using XPATH:

    driver.get('https://twitter.com/')    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Sign in']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

twitter_signin

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