Home > Net >  Clicking on LI link using Selenium Webdriver in Python
Clicking on LI link using Selenium Webdriver in Python

Time:09-12

New to Selenium here. Thanks for the help in advance!

I'm try to click on a li href inside an unordered list. Here is that unordered list.

<ul id="search_result"> 
    <li>1.  <a href="https://www.bookseriesinorder.com/lee-child/" rel="bookmark" 
             title="Permanent Link to Lee Child">Lee Child</a>
    <li>2.  <a href="https://www.bookseriesinorder.com/eileen-brady/" rel="bookmark" 
             title="Permanent Link to Eileen Brady">Eileen Brady</a>
    <li>3.  <a href="https://www.bookseriesinorder.com/lyla-lee/" rel="bookmark" 
             title="Permanent Link to Lyla Lee">Lyla Lee</a>
</ul>

And here is the code I'm trying to use.

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

options = Options()
options.page_load_strategy = 'normal' # normal, eager, none
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.get("https://www.bookseriesinorder.com/")

# Add code to wait until the search box has been loaded
try:
    search = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "s")))
except:
    driver.quit()    

# Enter the text to search for
author = "Lee Child"
search.send_keys(author)
search.send_keys(Keys.RETURN)

# Find the author link and click it
try:
    # search_results = WebDriverWait(driver, 10).until(
    #     EC.presence_of_element_located((By.XPATH,"//ul[@id='search_result']")))
    # search_results = WebDriverWait(driver, 10).until(
    #     EC.presence_of_element_located((By.XPATH,"//ul[@id='search_result']/a[contains(@href, 'lee-child')]"))).click    
    search_results = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH,"//ul[@id='search_result']/a[contains(@href, 'lee-child')]")))        
except:
    driver.quit()    

print (search_results)
driver.close()

I am trying to click on the link for Lee Child. I am trying to use XPATH and I can find the ul if I don't include the list item. But when I try to add the list item (so the search for Lee Child) I get the error "NameError: name 'search_results' is not defined".

Can anyone explain what the correct XPATH should be to find the link for Lee Child?

Thanks in advance for any and all guidance.

CodePudding user response:

You are almost there.
the only problem with your code is the XPath of the link you trying to click.
The a element is not a direct child of ul, so it should be // there, not /.
Also, it is preferably to use element_to_be_clickable rather than presence_of_element_located.
So, instead of

search_results = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,"//ul[@id='search_result']/a[contains(@href, 'lee-child')]")))

please try this:

search_results = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@id='search_result']//a[contains(@href, 'lee-child')]"))).click()
  • Related