简体   繁体   English

请建议 selenium+java 的等待功能

[英]Please suggest a wait function for selenium+java

I am automating an application were , Explicit wait is not working .我正在自动化一个应用程序,显式等待不起作用。

My requirement is to wait for a particular element until it is loaded/ visible or clickable to perform next action.我的要求是等待特定元素,直到它被加载/可见或可点击以执行下一步操作。

I tried all the expectedconditions in explicit but it failed.我显式地尝试了所有预期的条件,但它失败了。 only sleep is working.只有睡眠在起作用。

One thing that i have noticed is that , web browser is not load but page is loading and hence the explicit functionality doesnt work.我注意到的一件事是,Web 浏览器未加载,但页面正在加载,因此显式功能不起作用。

Could some one help me in this?有人可以帮助我吗?

Please find the attached请参考附件

Due to the fact that your question is rather general I can only provide a general answer at this point.由于您的问题相当笼统,我现在只能提供一般性的答案。 You could wait until your page as a whole has been loaded before you proceed with the testing.您可以等到整个页面加载完毕后再继续测试。 (I would suggest this as you claim to have issues where the browser does not seem to be completely ready when you proceed with your testing) (我建议这样做,因为您声称在进行测试时浏览器似乎没有完全准备好)

This can be done using the following code:这可以使用以下代码完成:

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"))

*This code is not mine it has been sourced from *此代码不是我的,它来自
Wait for page load in Selenium 在 Selenium 中等待页面加载

Explicit Wait ie WebDriverWait is proven & efficient and it is working just perfect in conjunction with ExpectedConditions . Explicit WaitWebDriverWait已被证明和高效,并且它与ExpectedConditions完美结合。

As your requirement is to wait for a particular element until it is loaded/ visible or clickable to perform next action you can use the below block of code :由于您的要求是等待特定元素,直到它被加载/可见或可点击以执行下一个操作,您可以使用以下代码块:

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.id("myDynamicElement")));
myDynamicElement.click();

This type of issues occurs when the web element takes little more time to load than usual time.当 Web 元素加载时间比平时多一点时,就会出现此类问题。 In this case, we have use polling mechanism in the given interval which is fluentWait.在这种情况下,我们在给定的时间间隔内使用轮询机制,即 fluentWait。 Below is the helpful code.下面是有用的代码。

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

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

There is three type of wait in selenium. selenium 有三种等待。

  • implicit Wait隐式等待
  • Explicit Wait显式等待
  • Fluent Wait流畅的等待

Implicit Wait隐式等待

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

Explicit wait显式等待

WebDriverWait wait = new WebDriverWait(WebDriver,TimeOut);

Fluent Wait流畅的等待

Wait wait = new FluentWait(WebDriver reference).withTimeout(timeout, SECONDS).pollingEvery(timeout, SECONDS).ignoring(Exception.class);

Fore more information how to use all wait with example please go to below URL.有关如何使用所有等待示例的更多信息,请转到以下 URL。

https://trickyautomationworld.blogspot.in/2018/02/implicit-wait-vs-explicit-wait-vs.html https://trickyautomationworld.blogspot.in/2018/02/implicit-wait-vs-explicit-wait-vs.html

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

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