2016-06-21 49 views
2

我试图自动化一个具有AJAX加载器的应用程序。它会抛出错误(org.openqa.selenium.WebDriverException:未知错误:元素在点(682,395)不可点击。其他元素将收到点击),当我尝试点击元素并且加载器处于活动状态时那时。该元素不可访问。由于AJAX加载器导致硒脚本失败

我已经为click()创建了一个包装方法,它确保AJAX完成并且可以单击元素。

我试过jQuery.active === 0.但是,在那之后,它也因加载失败。

所以,我在包装器方法中使用了isElementPresent来装载器。但是,这需要花费大量的时间。 isElementPresent需要6-8秒。我尝试加载器的className,cssSelector。但是,没有运气。

下面是我的代码:

public void WaitForAjax() throws InterruptedException { 

    while (true) { 

     Boolean ajaxIsComplete = (Boolean) ((JavascriptExecutor) driver) 
       .executeScript("return (document.readyState == 'complete') && (jQuery.active === 0);"); 
     if (ajaxIsComplete) { 
      if (isElementPresent(By.className("z-loading-indicator"))) { 
       continue; 
      } else { 
       break; 
      } 
     } 
     Thread.sleep(100); 
    } 
} 


    public void clickElement(WebElement element) throws InterruptedException { 
    WaitForAjax(); 
    Thread.sleep(500); 
    waitForElementVisible(element); 
    waitForElementClickable(element); 
    element.click(); 
    } 

请帮帮忙!

+0

您应该尝试使用'ExpectedConditions.invisibilityOfElementLocated'而不是所有这些.. –

+0

当您说“抛出错误”时,说出错是什么总是有用的。 – apokryfos

+0

@apokryfos:更新了问题。 –

回答

1

@Bhargav尝试下面的代码与xpath或任何其他seleniumBy方法。

WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath('xpath of the loader...'))); 

希望这有助于。

+0

已经尝试过了。也需要6-8秒钟点击。 :( –

+0

这很可能是您尝试执行操作的最佳方式。长时间延迟可能是因为您在click元素中存在的所有堆栈等待,或者可能是由于隐含和显式等待的混合。 。 – JeffC

相关问题