How to click the href?
I've tried
driver.find_element_by_xpath("/div/a[@id='switcher_plogin']")
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
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:
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