1

i have list of Hyperlinks in one page, when i click the links , they are redirecting to the new tab. how do i find out whether the page is loaded or not. i have used the do while loop for find out the element is enabled or not. but i am getting "no such element" only. could you please help on this.. Below is the piece of code. i have tried with Explicit wait also . but getting the same issue.

    WebElement element7 = driver.findElement(By.id("MenuControlBNY_MenuNavBar_MenuControlBNY_MenuNavBar_p11__Tree_item_2_cell"));
    if (element7.isEnabled())
    {
        element7.click();
        System.out.println(" Report is selected");
    }

    boolean element8 = false;
    int count = 0 ;
    do
    {
        element8 = driver.findElement(By.id("working")).isEnabled();
        System.out.println("Report is loaded");
        count = count+1;
        if(count == 1000)
        {
            break;
        }
    }while(element8 == true);
chengpohi
  • 14,064
  • 1
  • 24
  • 42
Krishna
  • 153
  • 4
  • 18
  • One of colleague has suggested need to use event Lister functions. Please suggest and help to resolve the issue – Krishna Apr 28 '15 at 08:45

3 Answers3

0

I use the following, its fairly self explanatory.

private static WebDriverWait wait = new WebDriverWait(driver, 60);
private static JavascriptExecutor js  = (JavascriptExecutor) driver;

public static void waitForPageLoaded() {
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    Boolean res = (js.executeScript("return document.readyState").equals("complete"));
                    System.out.println("[DEBUG] waitForPageLoaded: " + res);
                    return res;
            }
    });
}

EDIT: This will return true when the page is in a ready state ie the page is loaded. So you would call the above method prior to searching for your element.

Cathal
  • 1,318
  • 1
  • 12
  • 19
  • I'm open to correction on this, but i don't believe there are any built in methods that specifically wait for the page to load. – Cathal Apr 28 '15 at 11:45
  • line 59 of the [WebDriver](https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/WebDriver.java#L59) class. `"method will block until the load is complete"` – ddavison Apr 28 '15 at 11:49
  • 1
    actually, reading more into it, the [WebElement](https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/WebElement.java#L38) class, line 38 states that it most likely doesn't. (using the `WebElement#click()` method) – ddavison Apr 28 '15 at 11:50
  • While i am pasting the above method , i am getting two errors . – Krishna Apr 28 '15 at 12:09
  • in the until line is showing the error message as well as the js variable is not initialized in the script – Krishna Apr 28 '15 at 12:19
  • my bad, should have clarified that you need a new JavaScriptExecutor and WebDriverWait. Have added these to the code above. – Cathal Apr 28 '15 at 12:21
  • public static WebDriver setUpDriver(String browser) , i am initialized the browser using the setUpDriver method. if i used your code, the driver column is getting the error in both lines. could you please help on that. Below is my method code – Krishna Apr 28 '15 at 13:54
  • public static WebDriver setUpDriver(String browser) { WebDriver driver = null; if(browser.equalsIgnoreCase("ie")) { System.setProperty("webdriver.ie.driver","C:\\Data\\sw\\IEDriverServer.exe"); System.out.println(System.getProperty("webdriver.ie.driver")); driver = new InternetExplorerDriver(); } else if(browser.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.chrome.driver","C:\\Data\\sw\\chromedriver_win32(2)\\chromedriver.exe"); System.out.println(System.getProperty("webdriver.chrome.driver")); driver = new ChromeDriver (); } return driver; } – Krishna Apr 28 '15 at 13:55
  • i find it very hard to read that. and without knowledge of what the errors actually are, i cant help – Cathal Apr 28 '15 at 14:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76513/discussion-between-krishna-and-cathal). – Krishna Apr 29 '15 at 08:05
0

You can set an implicit wait which will wait for the elements when finding them for a 15 seconds

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

or explicitly:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("working")));
jmccure
  • 1,180
  • 7
  • 16
0

From the information you have provided, I am assuming you have clicked the link and the page is opened in a new tab and then you are getting nu such elment exception.

In this case you need to switch to the new tab using window handles and use explict wait for element.here is sample code

String parentWindow= driver().getWindowHandle();
element7.click();
Set<String> myset = driver().getWindowHandles();
myset.remove(parentWindow);
driver().switchTo().window((String)myset.toArray()[0]);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(locator)));
skumar
  • 180
  • 3
  • 17