0

What is the procedure for saving a hyperlink value i.e url into a varibale from the first <a href> in selenium that has a nested partial link text. For instance, on the screenshot a href with additional tags nested after it we have <a href> with the link (randomlink,mp4), some additional class followed by <svg> tag that has link text of " Download .MP4, 720p" after it.

In informal terms, randomlink.mp4 changes occasionally and I need to save it into a variable

The way the documentation is written, I have two choices:

Locating hyperlinks by text: either full link text that did not work since the link text is nested in SVG tag and selenium doesn't read it as part of the original element or partial link text which had the same issue.

1 Answers1

0

Given the HTML:

svg

The hyperlink is within the <use> tag, having a parent <svg>, which again is within it's parent <a> tag.


Solution

To click on the element with text Download .MP4 you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Download .MP4"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='randomlink.mp4'] > svg[name='download'][title='download'] > use"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='randomlink.mp4']//*[name()='svg' and @name='download']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352