I am using below python code using selenium. click is not working on anchor tag having href = "#"
我正在使用selenium下面的python代码。点击不适用于有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.
这是我正在使用的web html代码。
<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>
2 个解决方案
#1
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 :
根据您分享的HTML,似乎AUT基于JavaScript,因此要点击带有文本的链接作为验证数据,您必须引导WebDriverWait才能使元素可点击,您可以使用以下任一选项:
-
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()
LINK_TEXT:WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.LINK_TEXT,“验证数据”)))。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'和@ 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
#2
0
Try using a javascript executor to click your element.
尝试使用javascript执行器来单击您的元素。
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)
以上代码需要适应python(我不熟悉,但你明白了)
Hope this helps
希望这可以帮助
#1
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 :
根据您分享的HTML,似乎AUT基于JavaScript,因此要点击带有文本的链接作为验证数据,您必须引导WebDriverWait才能使元素可点击,您可以使用以下任一选项:
-
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()
LINK_TEXT:WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.LINK_TEXT,“验证数据”)))。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'和@ 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
#2
0
Try using a javascript executor to click your element.
尝试使用javascript执行器来单击您的元素。
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)
以上代码需要适应python(我不熟悉,但你明白了)
Hope this helps
希望这可以帮助