【问题标题】:Selenium Webdriver to wait until the asyn postback update panel request is completedSelenium Webdriver 等待异步回发更新面板请求完成
【发布时间】:2015-01-21 01:06:21
【问题描述】:
我有一个带有可点击列标题的表格。在第一次单击时,该列应按升序排序,而在第二次单击时,该列应按降序排序。
排序是使用异步回发更新面板实现的(我不确定它是如何完成的,它是一个 aspx 页面)。
我想使用 Selenium Webdriver 自动化排序功能。如何为未重新加载页面但仅重新加载表内容的页面实现 WAIT 条件。
waitForElementPresent 不起作用,因为单击标题时不会显示或隐藏新元素。
PS:需要Java实现。
【问题讨论】:
标签:
java
jquery
selenium
selenium-webdriver
【解决方案1】:
我添加了一个与 jquery 表 相关的示例程序。下面是代码的执行流程:
- 首先,它将导航到该站点。
- 由于我考虑了第二列“位置”,因此它将检索该列下的第一个文本。
- 然后,点击列标题“位置”进行升序排序
- 等待,10 秒(最长),直到第一个文本发生变化。
- 相应地打印结果。
- 再次单击列标题“位置”进行降序排序
- 等待,10 秒(最长),直到第一个文本发生变化。
-
相应地打印结果。
public class TestSortTable{
static WebDriver driver;
public static void main(String[] args){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.datatables.net/examples/basic_init/table_sorting.html");
//For Ascending in column "Position"
String result = clickAndWaitForChangeText(By.xpath("//table[@id='example']//th[2]"), By.xpath("//table[@id='example']//tr[1]/td[2]"), "Ascending");
if(result.contains("Fail"))
System.err.println(result);
else
System.out.println(result);
//For Descending in column "Position"
result = clickAndWaitForChangeText(By.xpath("//table[@id='example']//th[2]"), By.xpath("//table[@id='example']//tr[1]/td[2]"),"Descending");
if(result.contains("Fail"))
System.err.println(result);
else
System.out.println(result);
driver.close();//closing browser instance
}
//For clicking on header and waiting till the first text in the column changes
public static String clickAndWaitForChangeText(By Header_locator, By first_text_locator, String sortorder){
try{
String FirstText = driver.findElement(first_text_locator).getText();
System.out.println("Clicking on the header for sorting in: "+sortorder); //sortorder -> String representing sort order Ascending/Descending
driver.findElement(Header_locator).click();//Click for ascending/Descending
//Below code will wait till the First Text changes for ascending/descending
boolean b = new WebDriverWait(driver,10).until(ExpectedConditions.invisibilityOfElementWithText(first_text_locator, FirstText));
if(b==true){
return "Pass: Waiting Ends. Text has changed from '"+FirstText+"' to '"+driver.findElement(first_text_locator).getText()+"'";
}
else{
return "Fail: Waiting Ends. Text hasn't changed from '"+FirstText+"'.";
}
}catch(Throwable e){
return "Fail: Error while clicking and waiting for the text to change: "+e.getMessage();
}
}
}
注意:-您可以在代码中相应地使用 clickAndWaitForChangeText 方法来获取相关结果。
【解决方案2】:
你应该等到JQuery.active 返回 0。我的是用 c# 编写的。除此之外,我们还可以等待您知道将满足您的等待条件的特定元素。您可以使用fluentWait 或写您的custom wait 等待元素存在。
public void WaitForAjax()
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
}
编辑:Java 版本
public void waitForAjaxLoad(WebDriver driver) throws InterruptedException{
JavascriptExecutor executor = (JavascriptExecutor)driver;
if((Boolean) executor.executeScript("return window.jQuery != undefined")){
while(!(Boolean) executor.executeScript("return jQuery.active == 0")){
Thread.sleep(1000);
}
}
return;
}
直接取自here
【讨论】:
-
-
是的,实现非常相似。请参阅this 以在java 中获得更好和更广泛的解释