selenium web driver wait until page to refresh

微笑、不失礼 提交于 2019-12-23 04:19:13

问题


Below is the code snippet of my selenium test case, where i use select method to choose a value from the drop down . And the next step would be clicking on the submit . But by the time i try to click on the submit button the page is not refreshed(which will refresh the same page), throwing

element not clickable,StaleElementReference exception

. The only solution which works for me is thread.sleep().

I tried all the below options but had no luck :(

explicit wait(),wait.until(Exceptedcontions.visibility),element to be clickable() etc , tried all the solutions on th web .

I had to use thread.sleep() 3-4 times in a test case and i have around 100 test cases which is costing a lot of time .

Any working solutions where the web driver waits until the page gets completely refreshed and DOM loads completely before clicking on the submit button.

@Test
@Timeout(group = Group.SLOW)
public void testProvider() throws InterruptedException {

    proceedToProvider();
    new Select(driver.findElement(By.id("searchId"))).selectByVisibleText("Search");
    Thread.sleep(2000);
    driver.findElement(By.id("btnSubmit")).click();
    timeSplit("Search submitted");

Below is the error i see when i use other solutions.

org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (1289, 141). Other element would receive the click: (Session info: chrome=53.0.2785.116) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 168 milliseconds Build info: version: '2.49.1', revision: '808c23b0963853d375cbe54b90bbd052e2528a54', time: '2016-01-21 09:37:52' System info: host: 'ALAKASIMA01-W7L', ip: '10.145.45.233', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_73' Driver info: org.openqa.selenium.remote.RemoteWebDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=C:\Users\kasima01\AppData\Local\Temp\scoped_dir6628_12218}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=53.0.2785.116, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 8bf0b4cc7efc715015509f4be345d14d


回答1:


public void WaitForElementToLoad(String selector, String key, String seconds)
            throws ValidationException {

        Integer time = Integer.valueOf(seconds);

        WebElement element = null;

        while (time > 0) {

            try {
                element = getSpecificWebElement(selector, key);

                break;
            } catch (ValidationException e) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                time--;
            }
        }

        if (element == null && time <= 0) {
              // throw your own exception
        }
    }

Above method shows one of the basic ways to do this. getSpecificWebElement is a method can be defined by yourself to use findElementByXX and throw your own exception for catching. Pass the upper limit second you would wait for specific element.

There is also another solution - use driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

Update:

I think I'd better post getSpecificWebElement

private WebElement getSpecificWebElement(String selector, String key)
            throws ValidationException {
        WebElement element = null;

        String expression = "";

        By by = composeSelector(selector, key);

        try {
            element = FindElementBy(by);
        } catch (TimeoutException e) {

            throw new ValidationException("not found " + expression);
        } catch (Exception e) {
            throw new ValidationException(
                    Exceptions.getShortStackTraceAsString(e));
        }

        return element;
}


private WebElement FindElementBy(By by) {

        final By selector = by;

        ExpectedCondition<WebElement> ec = new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver driver) {

                return driver.findElement(selector);

            }

        };

        WebDriverWait wait = new WebDriverWait(driver, 10);

        return wait.until(ec);
    }

In this case the waiting time is enough, would be X(the passed seconds value) * 10.




回答2:


Try this will check entire page is loaded?

    static void waitForPageLoad(WebDriver wdriver) {
    WebDriverWait wait = new WebDriverWait(wdriver, 60);

    Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {

        @Override
        public boolean apply(WebDriver input) {
            return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");
        }

    };
    wait.until(pageLoaded);
}

Hope this will work for you.



来源:https://stackoverflow.com/questions/39783190/selenium-web-driver-wait-until-page-to-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!