Joaquin I
Joaquin I

Reputation: 23

Selecting a child with Selenium with Python

Is this part of the code right?

result2.find_element_by_xpath("./a[3]").click()

I want to get the third 'a' component from a 'div'

I am experiencing this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./a[3]"} (Session info: chrome=80.0.3987.149)

This is part of the code that includes the segment:

logear = browser.find_element_by_id('gs_hdr_tsi')
logear.click()
logear.send_keys('Connexins and pannexins in Alzheimer’s disease')
logear.send_keys(webdriver.common.keys.Keys.ENTER)
time.sleep(0.3)
result2 = browser.find_element_by_class_name('gs_fl')
result2.find_element_by_xpath("./a[3]").click() '''This line gives the error'''

Upvotes: 1

Views: 84

Answers (1)

0m3r
0m3r

Reputation: 12499

Try to add more time time.sleep(3) or wait for the element.

element = WebDriverWait(browser, 30).until(
    ec.element_to_be_clickable((By.XPATH, "./a[3]")))
element.click()

Per Comment

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait

chrome_browser = webdriver.Chrome()
chrome_browser.get("https://scholar.google.com/")

Search_input = WebDriverWait(chrome_browser, 30).until(
    ec.element_to_be_clickable((By.ID, "gs_hdr_tsi")))

Search_input.send_keys("Connexins and pannexins in Alzheimer’s disease" + Keys.RETURN)

try:
    chrome_browser.implicitly_wait(10)
    cited_by_link = chrome_browser.find_element_by_partial_link_text("Cited by")
    cited_by_link.click()
except Exception as e:
    print(e)

Upvotes: 1

Related Questions