3

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.

2 Answers2

0

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);
}
parishodak
  • 4,506
  • 4
  • 34
  • 48
  • Thanks. If I have multiple tabs in chrome? I tried and it checks only the tab opened with "new ChromeDriver();". Is there a way to check the url of the active tab and so execute some javascript in it? Hovewer in your while loop I tried to print the URL (System.out.println(currentURL)) and I saw that when going to a page, the loop "seems stop" until a specific moment (I suppose it's waiting for page loading): so the problem is still there :\ .. Anyway I can also do without this, but I need that Selenium acts on the active tab. Some idea? – Costantino Grasso Dec 31 '15 at 09:59
  • As you quoted, selenium has control over only the browser instances it has launched through new ChormeDriver(); If selenium has launched multiple tabs you can go to different tabs as explained in this post http://stackoverflow.com/questions/12729265/switch-tabs-using-selenium-webdriver – parishodak Dec 31 '15 at 10:12
  • Yes it's work. I looped through every tab and checked each url. So ok.. but I saw that Selenium stops until EVERY SINGLE PAGE is loaded.. is there a way to avoid this? I don't need they are fully loaded to execute js.. – Costantino Grasso Dec 31 '15 at 10:36
  • glad it helped. selenium is designed to wait till the page is loaded so that it can perform user actions on the rendered page. please upvote the answer if the solution is satisfactory. – parishodak Dec 31 '15 at 10:43
  • Yes I did, when I'll have 15 points of reputation the vote will be displayed. =D – Costantino Grasso Dec 31 '15 at 10:50
  • Wait I have another problem.. if I detach a tab and transform it in window, the .getWindowHandles() loop doesn't work anymore for it. Can I loop through multiple windows of chrome driver too? – Costantino Grasso Dec 31 '15 at 11:05
  • When you detach, it becomes a window instead of tab. so you wont be able to iterate with get tabs. Some help on how to work with multiple browser windows, pls refer: http://seleniumeasy.com/selenium-tutorials/perform-operations-on-new-window-using-webdriver – parishodak Dec 31 '15 at 11:24
  • Also discussing chain solutions in comments sections are discouraged in SO as it wont be helpful for the community who usually refers answer but not comments. please post a new question with more details. either me or someone will give answers. – parishodak Dec 31 '15 at 11:25
0

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

murali selenium
  • 3,847
  • 2
  • 11
  • 20