0

I'm trying to automate a download via Selenium using Python. The website I'm trying to download from has multiple options, with each option having a HTML HREF and an Excel HREF. So the site code looks like this:

       </ul>
            <li><a class="pnid-642 pv-pid-0 pvid-9972 cid-31">&nbsp;</a>24. Option 24
        <ul>
                            <li><table width='50%'><tr><td width='20%'>&nbsp;</td><td width='50%'>Select type</td><td width='15%'><A title='Html' HREF='/apps/carteras/genera_xsl_v2.0.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKV3Q3STg4Rk5oS1RYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span class="fa fa-file-code-o fa-2x" aria-hidden="true"'></span></a></td><td width='15%'><A title='Excel' HREF='/apps/carteras/genera_xsl2xls.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKV3Q3STg4Rk5oS1RYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span class="fa fa-file-excel-o fa-2x" aria-hidden="true"></span></a></td></tr></table></li>
            
        </ul>
            <li><a class="pnid-642 pv-pid-0 pvid-9972 cid-31">&nbsp;</a>25. Option 25
        <ul>
                            <li><table width='50%'><tr><td width='20%'>&nbsp;</td><td width='50%'>Select type<td width='15%'><A title='Html' HREF='/apps/carteras/genera_xsl_v2.0.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKVTBSRDZ5aVNsb2JYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span class="fa fa-file-code-o fa-2x" aria-hidden="true"'></span></a></td><td width='15%'><A title='Excel' HREF='/apps/carteras/genera_xsl2xls.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKVTBSRDZ5aVNsb2JYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span class="fa fa-file-excel-o fa-2x" aria-hidden="true"></span></a></td></tr></table></li>
            
        </ul>

I'm trying to automate the download of the Option 25 Excel file, but as you can see the Excel HREF are identical for each option on the website. Is there a way I can use Selenium to download only that Excel file?

Thanks

1
  • Possibly while handcrafting the HTML you have massacared the HTML beyond repair and solutions can't be really tested. Commented Mar 7, 2022 at 19:27

2 Answers 2

1

To identify the 25th Excel file use following xpath to identify.

driver.find_element(By.XPATH, "//li[contains(., '25. Option 25')]/ul/li//a[@title='Excel']").click()

If you want to make it dynamic you can create a method and pass the option text as parameter.

def DownloadFileOptions(optionName) :
    driver.find_element(By.XPATH, "//li[contains(., '{}')]/ul/li//a[@title='Excel']".format(optionName)).click()
      
 

 DownloadFileOptions('25. Option 25')
 DownloadFileOptions('24. Option 24')

I would suggest you to use webdriverwait() and wait for element to be clickable.

 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,  "//li[contains(., '25. Option 25')]/ul/li//a[@title='Excel']"))).click()

you need to import following library.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Sign up to request clarification or add additional context in comments.

Comments

0

You could try to find the element by the text it contains using

driver.find_elements_by_xpath("//*[contains(text(), '25. Option 25')]")

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.