0

I'm trying to use Selenium for python to click on the sign in link at the top of github. I've tried using find_element_by_link_text() but i get a NoSuchElementException. I then tried using find_element_by_xpath() and I got an ElementNotinteractableException. Here is the code for the first:

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://github.com')

signin = browser.find_element_by_link_text('Sign in')
signin.click()

and here's the code for the second.

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://github.com')

signin_link = browser.find_element_by_xpath('/html/body/div[1]/header/div/div[2]/div[2]/a[1]')
signin_link.click()

I even tried find_element_by_css_selector() but also got an ElementNotInteractableException. I don't understand what's going wrong. I don't feel like putting in the html, but if you go to github, it's just the sign in link at the very top right.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Oni Barolli
  • 65
  • 1
  • 7

3 Answers3

0

I think you are missing out to pass chromedriver path. Try this:

browser = webdriver.Chrome(r"C:\Users\...\chromedriver.exe")

Also, If you want to go to the login page, then I would recommend avoiding the long way root. What I mean is, the following code should directly take you to the login page:

browser.get('https://github.com/login') 

However, if you must know, how else you can click that element, try looping over "href" elements:

for el in browser.find_elements_by_tag_name("a"):
    if "/login" in el.get_attribute('href'):
        el.click()
0

To handle dynamic element induce WebDriverWait() and wait for element_to_be_clickable() and use following locator strtegies.

LINK_TEXT:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT,"Sign in")))
signin.click()

XPATH:

browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.XPATH,"//a[@href='/login']")))
signin.click()

CSS Selector:

browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,"a[href='/login']")))
signin.click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

To click() on the Sign in element at the top right corner of GitHub page https://github.com/ using Selenium, you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sign"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login']"))).click()
    
  • Using XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(., 'Sign')]"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352