[英]In selenium Anchor tag onclick function not working?
我有javascript onclick為:
<a href="#" id="download" onclick="Exceldownload('sites')">
<i class="fa fa-download card-down-icon" aria-hidden="true"></i>
</a>
在硒中我添加如下:
public void x() {
driver.findElement(By.xpath("//a[@onclick='Exceldownload("sites")']")).click();
}
但由於元素不可點擊,我收到錯誤。
可能性
如果一切正確,請嘗試點擊java腳本點擊,如代碼段所示。
WebElement element =driver.findElement(By.xpath("//a[@onclick='Exceldownload("sites")']")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
元素不可點擊
此問題可能有多種原因,如下所示:
也許像加載圖像這樣的其他元素會覆蓋元素,並在元素完全加載后消失。 然后你應該等到元素可點擊:
new WebDriverWait(driver, 60).until( ExpectedConditions.elementToBeClickable(By.id("download"))).click();
也許有多個元素存在於同一個定位器,不幸的是,您正在找到一個隱藏元素,該元素存在於頁面上但不可點擊。 然后,您需要使用唯一的定位器。
也許這是由於設計問題而發生的。 可能有一些其他元素覆蓋元素並且正在接收click事件。 然后,您可以使用JavascriptExecutor
作為替代解決方案:
((JavascriptExecutor)driver).executeScript( "arguments[0].click();", driver.findElement(By.id("download")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.