0

I have multiple link on webpage with button. I wanted to validate and ensure all link are working. Do not want to download each file and wait till gets finished. After successful download to check file whether its as expected?

Problem : My approach is to extract download link from web element and validate link itself. But failing to extract download link :(

Solutions I have tried :

Tried finding element using different locator strategy: like xpath, class_name, css_selector, id & even combination of two locators adv strategy too

Team, Let me know if you have faced and applied working solution for this issue. Thanks!

System :

Python Selenium Edge browser & Chrome Target : any browser

2 Answers 2

0

Working solution :

  • My targeted web has data-link attribute inside DOM for specific download button/element
  • I have preferred to use find_elements to get list of all available download link and iterated according to requirement
# Waiting for element
WebDriverWait(context.driver, 40, 0.4).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button:nth-child(2)')))

# Identified element which has download link under attribute "data-link" & iterated to print all available links 
elements = context.driver.find_elements(By.CSS_SELECTOR, 'button:nth-child(2)')
    for element in elements:
        print(" >> Download link is : ", element.get_attribute('data-link'))
0

You can extract the link from a web element by using the get_attribute method in Selenium. The method takes the attribute name as an argument, and in this case, the attribute you need to extract is the href attribute. Here's an example of how to do it in Python:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.example.com")

# Find the element with the link
link_element = driver.find_element_by_xpath("//a[@href='link_to_download']")

# Extract the link from the element
link = link_element.get_attribute("href")

# Verify the link
print(link)

In this example, the element is found using the find_element_by_xpath method and the xpath argument is set to a string that specifies the link. Once the element is found, the get_attribute method is used to extract the href attribute. The print statement is used to verify the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.