简体   繁体   English

如何使用 java 脚本在 selenium webdriver 中处理 Ajax

[英]How to handle Ajax in selenium webdriver by using java script

I am writing a code to test the webapplication but in my application We have AJAX Request the data will come by back end component.So it is taking some time to get data.我正在编写代码来测试 web 应用程序,但在我的应用程序中,我们有 AJAX 请求数据将来自后端组件。因此获取数据需要一些时间。

So can you please suggest me what is best method to use.那么你能建议我什么是最好的使用方法吗?

Advance Thanks Raju预先感谢 Raju

Set an implicit timeout on the driver在驱动程序上设置隐式超时

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

When an element is not immediatly found, it will wait for it during 10 seconds before throwing an exception.当没有立即找到一个元素时,它将在抛出异常之前等待 10 秒。 That should give your ajax calls enough time to call back.这应该给你的 ajax 调用足够的时间来回拨。

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

This will wait for that particular element(myDynamicElement) in your page which is loaded through ajax.这将等待页面中通过 ajax 加载的特定元素 (myDynamicElement)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM