'Web Scraping: How to click a specific href link in selenium python?

I wanted to automate a web page using webdriver(Python) wherein the script will click on the href link directly in order to navigate to other page. Couple of details in the specific tag:

<span class = "xyz", ui-sref ="abc", href= "/letters/letters_number/"> 
"hdf"
<i class = 'icons'>letters</i>
</span>


Solution 1:[1]

To click() on a the element with the href attribute you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[href=/letters/letters_number/] i.icons"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@href='/letters/letters_number/']//i[@class='icons']"))).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
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium