Home > OS >  How to click on a <a> tag with a href=# using selenium
How to click on a <a> tag with a href=# using selenium

Time:01-15

I am trying to download a file from a website but am not able to interact with the download button.
The button is linked to the <a> tag with a href=#.

<div >
        <a  href="#" onclick="creditingRates.download();" style="display: block;">Download CSV</a>
    </div>

I have tried the following but non seemed to work.

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//a[@href='#')]"))).click()

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='btn btn-download']/a[text()='Download CSV']))).click()

CodePudding user response:

  1. If you want to click the element you need to wait for element clickability, not just presence.
  2. From the shared HTML I see it's the a element who has , not a parent div element.
    I can't debug this, only to guess, so I'd try the following:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-download'][text()='Download CSV']"))).click()

Or maybe

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-download'][contains(,text(),'Download CSV')]"))).click()
  • Related