1
<a id="hlExcel" href="Reports\DC_REG_638123088090882623.xls" target="_blank"><img src="Images/Excel.gif" alt="Click to view a Excel version"></a>

How do I find and click on this excel file that I have to download everyday, given that the file name changes every day. I have tried this:-

link = browser.find_element_by_css_selector('["href="Reports"]')

link.click()

I am using python Selenium

Anthony
  • 542
  • 1
  • 8
  • 25

2 Answers2

1

Given the HTML:

<a id="hlExcel" href="Reports\DC_REG_638123088090882623.xls" target="_blank">
    <img src="Images/Excel.gif" alt="Click to view a Excel version">
</a>

To click on the clickable element even though the file name changes every day ideally 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, "a#hlExcel > img[alt='Click to view a Excel version']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='hlExcel']/img[@alt='Click to view a Excel version']"))).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
0

You could go for a partial match maybe. Is the DC_REG common in all file names ? If you know a pattern which will be part of the name irrespective of how many times the name changes, you could look for that pattern. Something like

link = browser.find_element_by_css_selector('a[href*="REG"][href$=".xls"]')

link.click()
  • Ho would I change this to take into account that the DC part is common. – Anthony Feb 18 '23 at 09:12
  • Try replacing REG with DC in my code besides the href – Pritam Mohanty Feb 18 '23 at 09:47
  • I am able to find the link with this code:- link = browser.find_element_by_css_selector('[href$=".xls"]') but now I receive this error "ElementNotInteractableException: Message:" but when I click on the file on the actual website it downloads – Anthony Feb 18 '23 at 09:48
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 24 '23 at 00:51