2

I am using below python code using selenium. click is not working on anchor tag having href = "#"

import time    
import unittest   
from selenium import webdriver   
from selenium.webdriver.common.by import By   
from selenium.webdriver.support.ui import Select   
from selenium.webdriver.common.keys import Keys   

driver = webdriver.Chrome("E:\chromedriver.exe")   
driver.get('file:///E:/Selenium/validateTest.html')     

driver.find_element_by_xpath("//a[@id='validateData']/i[text()=' Validate Data']").click()  

Here is the web html code that I am using.

<h1>Anchor tag</h1>

<a href="#" class="button js-button" role="button">Show content</a>

<a href="#" id="validateData" class="btn btn-red" onclick="document.write(5 + 6)"><i class="fa fa-binoculars" aria-hidden="true"></i> Validate Data</a>
Usman_Ahmed
  • 67
  • 1
  • 9

2 Answers2

1

Try using a javascript executor to click your element.

JavascriptExecutor js = (JavascriptExecutor) driver; 
WebElement elementToClick = driver.find_element_by_xpath("//a[@id='validateData']/i[text()=' Validate Data']");
js.executeScript("arguments[0].click();", elementToClick);

Above code needs to be adapted to python ( which i'm not familiar with, but you get the idea)

Hope this helps

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Pierre Baran
  • 160
  • 10
  • I am getting this error now: selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (88, 460). Other element would receive the click:
    ...
    – Usman_Ahmed May 10 '18 at 11:31
0

As per the HTML you have shared it seems the AUT is based on JavaScript, so to click on the link with text as Validate Data you have to induce WebDriverWait for the element to be clickable and you can use either of the following options :

  • LINK_TEXT :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Validate Data"))).click()
    
  • CSS_SELECTOR :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-red#validateData"))).click()
    
  • XPATH :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-red' and @id='validateData']"))).click()
    

Note : You will require 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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks DebanjanB. I am getting another now. selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (88, 460). Other element would receive the click:
    ...
    – Usman_Ahmed May 10 '18 at 13:00
  • @user3157061 _Element is not clickable_ is pretty common issue and you can solve it easily following the discussion [Element MyElement is not clickable at point (x, y) Other element would receive the click](https://stackoverflow.com/questions/44724185/element-myelement-is-not-clickable-at-point-x-y-other-element-would-receiv/44724688#44724688) or [Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el/44916498#44916498) – undetected Selenium May 10 '18 at 13:13