簡體   English   中英

無法使用 Selenium 單擊按鈕

[英]Unable to Click Button Using Selenium

我是硒的新手,我遇到了一個網頁,我想點擊一個嵌套在其他標簽中的按鈕,

HTML

 <div ng-show="hasAuthenticated &amp;&amp; !accessingInEU" class="account-buttons ng-isolate-scope signed-in" ng-class="{ 'signed-in': loggedIn }" style=""> <!-- ngIf: loggedIn --> <authenticated-button ng-if="loggedIn" class="ng-scope ng-isolate-scope" style=""> <icon-button icon-class="authenticated icon-account-male" on-click="authenticatedButtonCtrl.showAccountSettings()" class="ng-isolate-scope"> <button class="icon-container icon-button ng-binding" ng-click="onClick()" tabindex="0"> <span aria-hidden="true" ng-class="iconClass" class="authenticated icon-account-male" /> </button> </icon-button> </authenticated-button> </div>

這些是我嘗試過的代碼片段,

使用的示例代碼

WebDriverWait wait = new WebDriverWait(driver, 30);
boolean invisiable = wait.until(ExpectedConditions
        .invisibilityOfElementLocated(By.cssSelector(".icon-container.icon-button.ng-binding")));
if (invisiable) {
    WebElement ele = driver.findElement(By.cssSelector(".icon-container.icon-button.ng-binding"));
    ele.click();
}

當我嘗試上面的代碼時,出現下面的異常

線程“main”org.openqa.selenium.ElementNotInteractableException 中的異常:元素不可交互

我還使用 XPath 嘗試了以下代碼,但仍然無法單擊它。

driver.findElement(By.xpath("//authenticated-button//icon-button//button[contains(@class,'icon-container icon-button ng-binding')]")).click();

有人可以指導我解決這個問題嗎?

提前致謝,

您正在等待元素不可見,然后嘗試單擊它。 目前無法進行交互。

嘗試等待元素可點擊,然后點擊它:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions
        .elementToBeClickable(By.cssSelector(".icon-container.icon-button.ng-binding"))).click();

嘗試這個:

JavascriptExecutor executor = (JavascriptExecutor)driver;
                executor.executeScript("arguments[0].click();", ele);

而不是 Selenium 的 click() 方法

如果它不起作用,請嘗試使用 Action 類

new Actions(driver).moveToElement(ele).click().perform();

如果它也不起作用,那么我們將深入研究相關元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM