Home > Software engineering >  Selenium click button with <a> tag - intercepted exception
Selenium click button with <a> tag - intercepted exception

Time:01-30

I am trying to click a button on a website in Python using Selenium. The html for the button I want looks like this:

<a onclick="exportGridExcel()"><span ><i  title="Download Excel" aria-hidden="true"></i></span></a>

A more expanded version of the html is:

<div >
                            <div >
                                <ul>
    <li >                        
        <a href="#"  data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            <span >English</span>
            <span >EN</span>
            <img src="/etc/designs/wbrrdesign/clientlibs-wbrredsign/img/angle-down-gray.svg" alt="dropdown"></a>
        <ul >
            <li><a href="https://espanol.enterprisesurveys.org/es/enterprisesurveys">Español</a></li>
        </ul>
    </li>
</ul>

                            </div>
                        </div>

Then some other stuff before going to the button group and button I want:

<div >   
        <button onclick="onModifyQuery()" type="button" >Modify Query</button>
        <a onclick="exportGridExcel()"><span ><i  title="Download Excel" aria-hidden="true"></i></span></a>
        <a title="Undo to column removal" onclick="restoreColumn()" ><i  aria-hidden="true"></i></a>
    </div>

Part of my confusion is inexperience with this html with multiple classes and "i class".

EDIT:

If I try something like:

WebDriverWait(driver,300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick='exportGridExcel()']"))).click()

Then I get the error:

ElementClickInterceptedException: element click intercepted: Element <a onclick="exportGridExcel()">...</a> is not clickable at point (772, 11). Other element would receive the click: <div >...</div>

CodePudding user response:

The problem is that your page is automatically scrolled up and the excel download button is probably hidden by the top banner that contains the language selector. When Selenium tries to click on the excel download button, it finds the language selector instead.

I would suggest you to scroll the page up till the whole data table is visible

You can use something like this. Press HOME key to go to top of the page

from selenium.webdriver.common.keys import Keys
...
...
element = WebDriverWait(driver,300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick='exportGridExcel()']")))
element.send_keys(Keys.CONTROL   Keys.HOME)
element.click()

CodePudding user response:

Ideally clicking on the element <a onclick="exportGridExcel()"> as per your code trials inducing WebDriverWait for the elementToBeClickable() should have worked.

However, there seems to be a decendant <span>. So 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[onclick^='exportGridExcel'] > span.accordion-download"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'exportGridExcel')]/span[@class='accordion-download']"))).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
    

In another approach you can also use JavascriptExecutor as follows:

  • Using CSS_SELECTOR:

    element  = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='exportGridExcel'] > span.accordion-download")))
    driver.execute_script("arguments[0].click();", element) 
    
  • Using XPATH:

    element  = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'exportGridExcel')]/span[@class='accordion-download']")))
    driver.execute_script("arguments[0].click();", element)
    
  • Related