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 > </a>24. Option 24
<ul>
<li><table width='50%'><tr><td width='20%'> </td><td width='50%'>Select type</td><td width='15%'><A title='Html' HREF='/apps/carteras/genera_xsl_v2.0.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKV3Q3STg4Rk5oS1RYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span aria-hidden="true"'></span></a></td><td width='15%'><A title='Excel' HREF='/apps/carteras/genera_xsl2xls.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKV3Q3STg4Rk5oS1RYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span aria-hidden="true"></span></a></td></tr></table></li>
</ul>
<li><a > </a>25. Option 25
<ul>
<li><table width='50%'><tr><td width='20%'> </td><td width='50%'>Select type<td width='15%'><A title='Html' HREF='/apps/carteras/genera_xsl_v2.0.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKVTBSRDZ5aVNsb2JYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span aria-hidden="true"'></span></a></td><td width='15%'><A title='Excel' HREF='/apps/carteras/genera_xsl2xls.php?param=RWJybTl4VEV4MnlHc0VSQVd5T1VKVTBSRDZ5aVNsb2JYUDdaa1dFbDhoWkwzam53L3huQzBnPT0='><span 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
CodePudding user response:
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
CodePudding user response:
You could try to find the element by the text it contains using
driver.find_elements_by_xpath("//*[contains(text(), '25. Option 25')]")