1
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


# go to website
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chrome_options)
action = webdriver.ActionChains(driver)
driver.get('https://www.clinicalkey.com/#!/browse/book/3-s2.0-C2016100010X')

# look for "login" and click
loginclick = driver.find_element_by_xpath("//*[@id='header']/div[3]/ol/li[3]/a/span").click()

How come I'm not able to click on the login section on top right after navigating to the website given? I get an error:

Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='header']/div[3]/ol/li[3]/a/span"}
  (Session info: chrome=85.0.4183.83)
  File "C:\python\download.py", line 18, in <module>
    loginclick = driver.find_element_by_xpath("//*[@id='header']/div[3]/ol/li[3]/a/span").click()

Thank you !

1 Answer 1

1

Two reason:

1: Wait for element to load

2: As element is not visible on page use java script to click

driver.get('https://www.clinicalkey.com/#!/browse/book/3-s2.0-C2016100010X')
# look for "login" and click
loginclick=WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@id='header']/div[3]/ol/li[3]/a/span")))
driver.execute_script("arguments[0].click();", loginclick)

Output: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

awesome! thanks! Can you please explain what does this (driver.execute_script("arguments[0].click();") mean?
As your element is hidden on the page, normal selenium python binding is not able to click it. So we have used Javascript to locate the element and click it. As to interact with web elements our normal script first converted into Javascript by binding and then perform actions as instructed. Sometime when it is not able to perform desired action we can use direct Javascript to do so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.