1
  1. I need to wait for a page 'popup overlay' to load before scrolling to the relevant web element.

  2. The following code below is successful when locating and scrolling to a web element but i want to avoid Thread.sleep.

    public void scrollToElementByLocator(WebElement element) throws InterruptedException {
    
    Thread.sleep(4000);
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
    
    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, -400)");
    

3. Can anyone advice on another method to wait for a page to load and then scroll to the intended webelement without using thread.sleep?

enter image description here

Many thanks for your help

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Gbru
  • 1,065
  • 3
  • 24
  • 56
  • Try to `click()` on any element while page is loading. You should get exception, something like `element is not clickable at the point ... other element received the click...` Show the part of exception with `HTML` of element that actually received the click (the modal window that overlay your target element) – Andersson Dec 13 '16 at 15:28
  • Below link will work for your need http://stackoverflow.com/questions/15122864/selenium-wait-until-document-is-ready – Gobi Dec 13 '16 at 16:54

3 Answers3

1

A solution i use for waiting on page load is waiting for the page title to match what i expect. For example:

public Boolean waitForPageIsLoaded(String title) {
     return new WebDriverWait(driver, 10).until(ExpectedConditions.titleIs(title));
}

Of course if you know what WebElement you are waiting for there are plenty of ExpectedConditions you can take advantage of eg, visibilityOf()

Cathal
  • 1,318
  • 1
  • 12
  • 19
1

These are a pain. Ideally you would have access to the devs and they could tell you what elements appear/disappear that you can wait for. In this case, I'm assuming you don't have access to them.

What I try to do is trigger the action, quickly right-click and choose Inspect element, and see what elements are popping up. If you are lucky, the dialog, etc. stays up and it's easy to find. In cases like this, they happen so briefly that it makes it really hard. What I did was to do the actions above to the point where I was triggering it on/off and watching the DOM for elements to appear/disappear. I finally got in the right place and found this using screencap and OCR

<div class="modal in" id="loading-modal" data-backdrop data-keyboard="false" tabindex="-1" role= "dialog" aria-hidden="true" style="z-index: 1100; top: 475px; display: block; padding-right: 17px;" modal-dialog">
<div> id="loading-page-backdrop" class="in"></div> 

There are a couple of DIVs there, one of them is very likely the element you are looking for and both have IDs so they should be easy to get ahold of and wait for them to be invisible.

// wait for modal to disappear
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.id("loading-modal")));
// do stuff

Even if these aren't the elements you are looking for, you should be able to use this technique to find the ones you want.

BTW, I write automation in Java and I've never needed to scroll the window... it just does it for me. Have you tried the scenario without the scroll code?

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • thank you so much for your help, Im currently using firefox 47.0.1 and for some reason if i dont scroll to the relevant location on the page where the WebElement is located, before trying to perform any actions I get presented with the message unable to click at point. Are screencap and OCR a type of software? thanks again – Gbru Dec 14 '16 at 08:49
  • Screencap is just a screenshot... screencapture. OCR is software that pulls text from images. I did that just so I could post the text here, you shouldn't actually need that part. Just take a screenshot so that if it disappears quickly, you can still go back and see the HTML. – JeffC Dec 14 '16 at 14:18
  • If you found this answer (or any answer) useful, please upvote it. If this answered your question, please mark it as accepted. Thanks! – JeffC Dec 20 '16 at 14:52
0

To verify the page is loaded completely, loop through the below statement until it returns the value "complete"

(String) ((JavascriptExecutor) driver).executeScript("return document.readyState");

For more details refer here

SVS
  • 1
  • 1
  • Better than looping, would be to try Selenium's `new WebDriverWait().until(ExpectedConditions...)...` – MarkHu Sep 21 '21 at 16:15