Issue
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:
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
Solution
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.
Answered By - Furkan Ozalp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.