There are many good answers on this page.
- I usually start with maximize.window (), in fact I do this in the driver factory or wherever you initialize the driver. This is what is done by default - always.
- This is usually an expectation of an element due to some JavaScript delay.
Both are discussed in various details above. The answer I did not see was ScrollToElement. It looks like you are processing a list of items, and when processing you create more items, check boxes. This can cause items in your list to move from the visible page. Sometimes you can see an element with the naked eye, but you just can’t click on it. When processing lists, sometimes you have to interrupt scrolling.
- Set a breakpoint and check if the item you are using is on the edge of the window, top / bottom right / left. Sometimes, when this is the case, you cannot reach it with selenium, but you can manually click the mouse.
Since I came across this, I created PageScroll.java and put my scroll scripts there. Here are a few methods from this class:
public static void scrollToTop(WebDriver driver) { ((JavascriptExecutor) driver) .executeScript("window.scrollTo(0,0)"); } public static void scrollToBottom(WebDriver driver) { ((JavascriptExecutor) driver) .executeScript("window.scrollTo(0, document.body.scrollHeight)"); } public static void scrollToElementTop(WebDriver driver, WebElement element) { ((JavascriptExecutor) driver).executeScript( "arguments[0].scrollIntoView(true);", element); } public static void scrollToElementBottom(WebDriver driver, WebElement element) { ((JavascriptExecutor) driver).executeScript( "arguments[0].scrollIntoView(false);", element); }
see Scroll Element to View with Selenium for more examples
mancocapac Jul 01 '19 at 0:34 2019-07-01 00:34
source share