javascript - 在 selenium Anchor 标签中,onclick 函数不起作用?

标签 javascript selenium selenium-webdriver automated-tests selenium-chromedriver

我有 javascript onclick 作为:

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

在 selenium 中我是这样添加的:

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

但我收到错误消息,因为元素不可点击。

最佳答案

可能性

  1. 页面中具有相同 xpath 的重复 Web 元素。
  2. 元素可能在框架中。你需要切换到框架
  3. 尝试在页面加载之前访问网络元素。请等待一些时间。

如果一切正确,请尝试点击代码片段中显示的 java 脚本。

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

关于javascript - 在 selenium Anchor 标签中,onclick 函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42201000/

相关文章:

javascript - 同步动态列表中的问题与来自服务器的请求列表(VueJs)

javascript - AJAX 导致 700 个 Google 抓取错误?

javascript - Python selenium,我怎样才能删除一个元素?

java - 如果afterClass方法失败,Gradle测试报告将显示测试失败

java - Selenium 2 : something wrong with “driver. findElement(By....);

javascript - 我怎样才能改变这个缩小的 if 语句?

javascript - Virtuoso SPARQL 查询在 JavaScript 获取时速度慢

c# - 无法在 C# 中使用 WebDriverWait 显式等待汉堡菜单

python - 如何告诉谷歌我接受cookies python

java - Selenium 中的 Apache POI 文本格式不佳