Selenium 中的 Ajax 弹出窗口

Posted

技术标签:

【中文标题】Selenium 中的 Ajax 弹出窗口【英文标题】:Ajax popup in Selenium 【发布时间】:2012-10-11 11:01:22 【问题描述】:

是否可以使用

捕获/记录灯箱(Ajax)

硒 IDE

?

【问题讨论】:

解释你到底想“捕获”什么。快速浏览一下 JS 会发现它使用静态 ID 和类作为弹出窗口,所以是的,这是完全可能的。 【参考方案1】:

不确定 Selenium IDE 是否以您期望的形式捕获/记录 Light Box (Ajax)。 见

Selenium IDE 能够捕获导致 LightBox 出现的点击事实。但是很难注册从服务器获取所有 AJAX 所需的时间。 作为 QA selenium 自动化(在 java 上编写),我更喜欢另一种方法。 你可以使用 webDriverWait 条件:

  WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element")));

如here所述

或者你可以调用fluentWait机制:

public WebElement fluentWait(final By locator)
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

        WebElement foo = wait.until(
new Function<WebDriver, WebElement>() 
            public WebElement apply(WebDriver driver) 
                        return driver.findElement(locator);
                
                
);
                           return  foo;                   ;

这种流畅的等待方法返回您找到的可以操作的 webElement。 用法:

String xPathElement ="...blablab.....";
WebElement found  = fluentWait(By.xpath(xPathElement));
found.click();
//found.getText().trim():

希望对你有帮助

【讨论】:

以上是关于Selenium 中的 Ajax 弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章