frozenjs ajax,javascript - Why Ajax call is frozen during WebDriverWait (Selenium) - Stack Overflow

The implicit wait in Selenium 2 might not work for Ajax elements. We recommend you to use any one of the following workaround to handle Ajax elements.

One approach is to use FluentWait and a Predicate available with Selenium2. The advantage of this approach is that element polling mechanism is configurable. The code example below waits for 1 second and polls for a textarea every 100 milliseconds.

FluentWait fluentWait = new FluentWait(By.tagName("TEXTAREA"));

fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);

fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS);

fluentWait.until(new Predicate() {

public boolean apply(By by) {

try {

return browser.findElement(by).isDisplayed();

} catch (NoSuchElementException ex) {

return false;

}

}

});

browser.findElement(By.tagName("TEXTAREA")).sendKeys("text to enter");

Another approach is to use ExpectedCondition and WebDriverWait strategy. The code below waits for 20 seconds or till the element is available, whichever is the earliest.

public ExpectedCondition visibilityOfElementLocated(final By by) {

return new ExpectedCondition() {

public WebElement apply(WebDriver driver) {

WebElement element = driver.findElement(by);

return element.isDisplayed() ? element : null;

}

};

}

public void performSomeAction() {

..

..

Wait wait = new WebDriverWait(driver, 20);

WebElement element = wait.until(visibilityOfElementLocated(By.tagName("a")));

..

}

my particular adjustment of fluentWait in the way I use is in code:

public WebElement fluentWait(final By locator, WebDriver driver) {

Wait wait = new FluentWait(driver)

.withTimeout(30, TimeUnit.SECONDS)

// .pollingEvery(5, TimeUnit.SECONDS)

.pollingEvery(1, TimeUnit.SECONDS)

// .ignoring(NoSuchElementException.class);

.ignoring(org.openqa.selenium.NoSuchElementException.class);

WebElement foo = wait.until(

new Function() {

public WebElement apply(WebDriver driver) {

return driver.findElement(locator);

}

}

);

return foo;

}

And the call will look like:

WebElement abracadabra = fluentWait(By.cssSelector('.elemSelector'));

For hanling alerts and getting to know whether they are shown or not, I would recommend smth like:

public boolean isAlertPresent(WebDriver driver) {

boolean presentFlag;

try {

// Check the presence of alert

Alert alert = driver.switchTo().alert();

// Alert present; set the flag

presentFlag = true;

// if present consume the alert

// alert.accept();

} catch (NoAlertPresentException ex) {

// Alert not present

// ex.printStackTrace();

presentFlag = false;

}

return presentFlag;

}

Hope this helps you.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值