1

Html:

<div class="xxxx">
<a href="ooooo.pdf"></a>
</div>

Python selenium code trials:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
print(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.xxxx a"))).get_attribute('href'))

Output:

aaaaa.pdf

How print ooooo.pdf and aaaaa.pdf?

I want to print more links, what should I do?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
ADAM
  • 63
  • 1
  • 1
  • 6
  • See here https://stackoverflow.com/questions/34759787/fetch-all-href-link-using-selenium-in-python – Hrvoje Apr 20 '22 at 12:33
  • Does this answer your question? [Fetch all href link using selenium in python](https://stackoverflow.com/questions/34759787/fetch-all-href-link-using-selenium-in-python) – Hrvoje Apr 20 '22 at 12:34

1 Answers1

0

element_to_be_clickable() returns a single WebElement hence href attribute of only the first matching element is printed.


Solution

To extract all the href attribute values you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.xxxx a")))])
    
  • Using XPATH:

    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='xxxx']//a")))])
    

Alternative

As an alternative you can also try:

  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("href") for my_elem in driver.find_elements(By.CSS_SELECTOR, "div.xxxx a")])
    
  • Using XPATH:

    print([my_elem.get_attribute("href") for my_elem in driver.find_elements(By.XPATH, "//div[@class='xxxx']//a")])
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352