简体   繁体   中英

Avoid the execution stop while browsing in Selenium WebDriver

I need help for this thing that's driving me crazy. I want to check the browser url in and endless loop, waiting a little (Thread.Sleep) between a loop and another, to not overload the CPU. Then, if the browser url is what I need, I want to add/change/remove an element through Javascript before the page is fully loaded, otherwise the person who uses this could see the change. (I don't need help for the javascript part) But there's a problem: it seems that in Selenium Webdriver when I navigate to a page (with .get(), .navigate().to() or also directly from the client) the execution is forced to stop until the page is loaded. I tried to set a "fake" timeout, but (at least in Chrome) when it catches the TimeoutException, the page stops loading. I know that in Firefox there's an option for unstable loading, but I don't want to use it because my program isn't only for Firefox.

public static void main(String[] args) throws InterruptedException {        
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.MILLISECONDS); // Fake timeout
    while (true) {
        try {
            // If the url (driver.getCurrentUrl()) is what I want, then execute javascript without needing that page is fully loaded
            // ...
            // ...               
        }
        catch (TimeoutException e) {
             // It ignores the Exception, but unfortunately the page stops loading.
        }
        Thread.sleep(500); // Then wait some time to not overload the cpu
    }
}

I need to do this in Chrome, and if possible with Firefox and Internet Explorer. I'm programming in Java. Thanks in advance.

Selenium is designed to stop once the webpage is loaded into the browser so that it can proceed with execution.

In your case there are two options:

1) If the browser url will change automatically (ajax) at an arbitrary time, then just keep getting browser url until your condition satisfies.

while(currentURL.equals("Your Condition")){
  currentURL = driver.getCurrentUrl();
  Thread.sleep(2000);
}

2) If the browser needs to be refreshed use the refresh method in a loop until you get your desired url

while(currentURL.equals("Your Condition")){
    driver.navigate().refresh();
    currentURL = 
    Thread.sleep(2000);
}

As know, if user tried with driver.get("url");, selenium waits until page is loaded (may not be very long). so if you want to do some thing on navigate to URL without waiting total load time use below code instead of get or navigate

    JavascriptExecutor js=(JavascriptExecutor)driver;
    js.executeScript("window.open('http://seleniumtrainer.com/components/buttons/','_self');");

After this used

driver.findElement(By.id("button1")).click();

to click on button but i am getting no such element exception, so i am expecting its not waiting for page load. so times page loading very quick and click working fine.

i hope this will help you to figure it out your issue at start up. for loop i hope solution already provided.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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