硒阿贾克斯等待,如果阿贾克斯没有返回的元素? [英] Selenium Ajax wait if Ajax returns no elements?

查看:143
本文介绍了硒阿贾克斯等待,如果阿贾克斯没有返回的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一些关键部分inputfield。当焦点从该元素去掉,一个Ajax请求被发送到服务器,如果这个值我进入是有效的。 如果是,什么也不会发生,如果不出现错误信息。 有一对夫妇这些领域。

I am sending some keys to some inputfield. When focus is removed from this element, an ajax request is sent to server if this value I entered is valid. If it is, nothing happens, if not an error message occurs. There are a couple of these fields.

当我说:

driver.findElementById(firstId).sendKeys(firstValue);
driver.findElementById(secondId).sendKeys(secondValue);

第二个值将不会被发送到第二元件,因为将有在平均时间非常短ajax请求。但由于该值是确定(firstValue),它不会弹出任何文本或其他任何东西。

The second value will not be sent to the second element because there will be a very short ajax request in the mean time. But since the value is ok (firstValue) it will not bring up any text or anything else.

我怎么能告诉硒等待这个Ajax来完成?我不希望使用的Thread.sleep

How can I tell Selenium to wait for this ajax to finish? I do not want to use Thread.sleep.

推荐答案

硒不会等待AJAX​​加载。它会自动等待页面加载。为了等待AJAX​​类型的负载,你必须使用隐显等待。

Selenium won't wait for AJAX loading. It automatically waits for a page loading. To wait for AJAX type loading you have to use Implicit and Explicit wait.

您可以使用隐式等待,并明确等待,等待一个特定的Web元素,直到它出现在页面中。你可以定义,那就是等待周期取决于应用程序。

You can use Implicit Wait and Explicit Wait to wait for a particular Web Element until it appears in the page. The wait period you can define and that is depends upon the application.

明确等待:

这是明确的等待是code定义等待一定的条件,在code进一步处理之前发生。如果条件实现它将终止等待,并继续进一步的步骤。

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. If the condition achieved it will terminate the wait and proceed the further steps.

code:

WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(strEdit)));

WebElement myDynamicElement = (new WebDriverWait(driver, 30))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});

这最多等待30秒抛出一个TimeoutException之前或者如果它发现的元素将在0返回它 - 30秒。默认情况下WebDriverWait调用ExpectedCondition每500毫秒,直到它成功返回。一个成功的回报是ExpectedCondition类型是所有其他类型的ExpectedCondition布尔返回true,否则返回null值。

This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

您可以使用ExpectedConditions类,因为你需要的应用程序。

You can use ExpectedConditions class as you need for the application.

隐等待:

这是隐式的等待是告诉webdriver的努力寻找一种以上的元素,如果他们没有立即可用时轮询的DOM一定量的时间

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available

code:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

有一点要记住的是,一旦隐含的等待设置 - 它仍将是webdriver的对象实例的生命

One thing to keep in mind is that once the implicit wait is set - it will remain for the life of the WebDriver object instance

有关更多信息使用这个链接<一个href="http://seleniumhq.org/docs/04_webdriver_advanced.jsp">http://seleniumhq.org/docs/04_webdriver_advanced.jsp

For more info use this link http://seleniumhq.org/docs/04_webdriver_advanced.jsp

您可以在AJAX装载过程中使用这些等待的时间。

我希望这会有所帮助。

这篇关于硒阿贾克斯等待,如果阿贾克斯没有返回的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆