Home > Mobile >  Selenium click button while accounting for header name
Selenium click button while accounting for header name

Time:12-08

I am new to selenium and would like to download a specific file from a website. There are multiple class tags whereas each has the heading in the form "[Year] Annual Report". The following is for the year 2017. How can I click the "Download" Button while ensuring that this is the button for a specific year (e.g., 2017 and not 2015)?

<li  style="display: flex;">
                                <img src="/HostedData/_ara_prvw/68711.jpg" alt="1-800-FLOWERS.COM" title="1-800-FLOWERS.COM" width="182" height="235">
                                <div >
                                    <span >2017 Annual Report</span>
                                    <span >
                                        <a href="/HostedData/AnnualReportArchive/1/NASDAQ_FLWS_2017.pdf" title="View 2017 Annual Report (PDF)" target="_blank">View Annual Report</a>
                                    </span>
                                    
                                    <span >
                                        <a href="/HostedData/AnnualReportArchive/1/NASDAQ_FLWS_2017.pdf" download="">Download</a>
                                    </span>
                                </div>
                            </li>

CodePudding user response:

For a page you shared you can use the following XPath: //span[@class='btn_archived download'][.//a[contains(@href,'2017')]]
So, the Selenium python command to click it can be as following:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='btn_archived download'][.//a[contains(@href,'2017')]]"))).click()

Actual code may contain scrolling and more required actions. According to actual code flow.

  • Related