0

How to click the href? I've tried

driver.find_element_by_xpath("/div/a[@id='switcher_plogin']")

I got an error like this:enter image description here I'd appreciate any help. Thanks.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
hj gh
  • 29
  • 3
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Jul 13 '19 at 23:19
  • Use by id, you don't need the XPath here. Is it in an IFRAME? Have you tried adding a wait for clickable? – JeffC Jul 13 '19 at 23:20

3 Answers3

0

To click on the link Use WebDriverWait and element_to_be_clickable along with following option.

Xpath:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@id='bottom_qlogin']/a[@id='switcher_plogin']"))).click()

CSS Selector:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#bottom_qlogin #switcher_plogin"))).click()

You need to imports following to execute above code.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
KunduK
  • 32,888
  • 5
  • 17
  • 41
0
  1. Make sure that the link doesn't belong to iframe, otherwise you will have to call WebDriver.switch_to_frame function first in order to change the context to the iframe where the link lives
  2. Make sure that the link doesn't belong to Shadow DOM otherwise you will have to locate the relevant ShadowRoot element first using execute_script function, cast the result to the WebElement and use WebElement's find() function to locate the link
  3. It might be the case the link is not immediately present in DOM so you might need to introduce Explicit Wait
  4. If there are no other elements having ID attribute of switcher_plogin you might want to reconsider the locator strategy and stick to the ID instead of XPath.

Suggested code change:

replace this:

driver.find_element_by_xpath("/div/a[@id='switcher_plogin']")

with this:

WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "switcher_plogin")))

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

The desired element is a JavaScript enabled element so to click() on the element you need to induce WebDriverWait for the desired element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.link#switcher_plogin"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='link' and @id='switcher_plogin']"))).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
    

Note: You can find a relevant discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

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