java - Selenium : Handling Loading screens obscuring the web elements.(Java)

标签 java selenium webdriver selenium-webdriver

我正在为网页编写一个自动化测试用例。这是我的场景。 我必须单击并输入 html 表单中的各种 Web 元素。但是,有时在文本字段上输入内容时,会出现一个ajax加载图像,使我想要交互的所有元素变得模糊。因此,我在单击实际元素之前使用网络驱动程序等待,如下所示,

WebdriverWait innerwait=new WebDriverWait(driver,30);
innerwait.until(ExpectedConditions.elementToBeClickable(By.xpath(fieldID)));
driver.findelement(By.xpath(fieldID)).click();

但是等待函数会返回该元素,即使它被另一个图像模糊并且不可点击。但是 click() 会抛出异常

Element is not clickable at point (586.5, 278).
Other element would receive the click: <div>Loading image</div>

在与任何元素交互之前,我是否必须每次都检查加载图像是否出现?(我无法预测加载图像何时出现并使所有元素模糊。) 有什么有效的方法来处理这个问题吗? 目前我正在使用以下函数等待加载图像消失,

public void wait_for_ajax_loading() throws Exception
{
    try{
    Thread.sleep(2000);
    if(selenium.isElementPresent("id=loadingPanel"))
    while(selenium.isElementPresent("id=loadingPanel")&&selenium.isVisible("id=loadingPanel"))//wait till the loading screen disappears
    {
         Thread.sleep(2000);
         System.out.println("Loading....");

    }}

    catch(Exception e){
        Logger.logPrint("Exception in wait_for_ajax_loading() "+e);
        Logger.failedReport(report, e);
        driver.quit();
        System.exit(0);
    }

}

但是我不知 Prop 体什么时候调用上面的函数,在错误的时间调用会失败。有没有有效的方法来检查元素是否确实可点击?或者加载图像存在?

谢谢..

最佳答案

鉴于您所描述的情况,您必须验证以下两个条件之一:

  1. 您要点击的元素是否可点击?
  2. 阻止点击的原因仍然存在吗?

通常,如果WebDriver能够找到该元素并且它是可见的,那么它也是可点击的。知道可能阻止它的可能原因后,我宁愿选择验证这些原因。此外,它在测试代码中会更具表现力:您可以清楚地看到您在等待什么,您在单击元素之前正在检查什么,而不是在没有明显原因的情况下检查“可点击性” 。我认为它可以让一个人(阅读测试的人)更好地理解实际发生的情况(或可能发生的情况)。

尝试使用此方法检查加载图像是否不存在:

// suppose this is your WebDriver instance
WebDriver yourDriver = new RemoteWebDriver(your_hub_url, your_desired_capabilities);

......
// elementId would be 'loadingPanel'
boolean isElementNotDisplayed(final String elementId, final int timeoutInSeconds) {
    try {
        ExpectedCondition condition = new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(final WebDriver webDriver) {
                WebElement element = webDriver.findElement(By.id(elementId));
                return !element.isDisplayed();
            }
        };
        Wait w = new WebDriverWait(yourDriver, timeoutInSeconds);
        w.until(condition);
    } catch (Exception ex) {
        // if it gets here it is because the element is still displayed after timeoutInSeconds
        // insert code most suitable for you
    }
        return true;
}

也许您需要根据您的代码对其进行一些调整(例如,在页面加载时查找该元素一次,然后仅检查它是否显示)。

如果您不确定加载图像何时出现(尽管我想您会这样做),则应在每次单击由于加载图像而变得“不可单击”的元素之前调用此方法。如果加载图像存在,该方法将在其消失后立即返回true;如果它没有在“timeoutInSeconds”时间内消失,该方法将执行您选择的操作(例如,抛出带有特定消息的异常)。

你可以将它包装在一起,如下所示:

void clickSkippingLoadingPanel(final WebElement elementToClick) {
    if (isElementNotDisplayed('loadingPanel', 10)) {
        elementToClick.click();
    }
}

希望有帮助。

关于java - Selenium : Handling Loading screens obscuring the web elements.(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17259393/

相关文章:

ajax - 如何让 Selenium WebDriver 等待一段时间?

java - 对 Java 聊天应用程序进行单元测试

java - Log4j:socketappender - 当远程主机关闭时如何捕获本地文件中的日志

java - 在没有站点 :site? 的情况下从 Maven 生成 Findbug HTML 报告的任何简单方法

Java Selenium : how can I get the HTML of a webpage without first loading the page?

selenium - 如何在 Chrome 中运行 Selenium WebDriver 测试用例

java - 从 Eclipse 中将带有 JavaFX WebView 组件的 SWT 作为 Runnable JAR 导出导致 NullPointerException

python - Selenium 无效参数异常

selenium - 使用页面对象模式执行操作的多种方式

java - 具有 ID 并包含文本的 Xpath 表达式