简体   繁体   English

在selenium中锚点标签onclick功能不起作用?

[英]In selenium Anchor tag onclick function not working?

I have javascript onclick as: 我有javascript onclick为:

<a href="#" id="download" onclick="Exceldownload('sites')">
    <i class="fa fa-download card-down-icon" aria-hidden="true"></i>
</a>

In selenium I added like this: 在硒中我添加如下:

public void x() {
    driver.findElement(By.xpath("//a[@onclick='Exceldownload("sites")']")).click();
}

But I am getting error as element is not clickable. 但由于元素不可点击,我收到错误。

Possibilities 可能性

  1. Duplicate web element with same xpath in the page. 页面中具有相同xpath的重复Web元素。
  2. Element may be in frame. 元素可以在框架中。 You need to switch to frame 你需要切换到框架
  3. Trying to access the web element before page is loading.Give some wait time. 尝试在加载页面之前访问web元素。给一些等待时间。

If everything is correct try by clicking through java script click as shown in snippet. 如果一切正确,请尝试点击java脚本点击,如代码段所示。

 WebElement element =driver.findElement(By.xpath("//a[@onclick='Exceldownload("sites")']")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element); 

element is not clickable 元素不可点击

This issue can have multiple reasons as below: 此问题可能有多种原因,如下所示:

  • Maybe some other element like a loading image overlays the element and disappears after the element is fully loaded. 也许像加载图像这样的其他元素会覆盖元素,并在元素完全加载后消失。 Then you should wait until the element is clickable: 然后你应该等到元素可点击:

     new WebDriverWait(driver, 60).until( ExpectedConditions.elementToBeClickable(By.id("download"))).click(); 
  • Maybe there are multiple elements present with the same locator, and unfortunately you're locating a hidden element which is present on the page but not clickable. 也许有多个元素存在于同一个定位器,不幸的是,您正在找到一个隐藏元素,该元素存在于页面上但不可点击。 Then you need to use a unique locator. 然后,您需要使用唯一的定位器。

  • Maybe this occurred due to a designing problem. 也许这是由于设计问题而发生的。 It might be possible some other element overlays the element and is receiving the click event. 可能有一些其他元素覆盖元素并且正在接收click事件。 Then you can use JavascriptExecutor as an alternative solution: 然后,您可以使用JavascriptExecutor作为替代解决方案:

     ((JavascriptExecutor)driver).executeScript( "arguments[0].click();", driver.findElement(By.id("download"))); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM