There are some instances during automation using Selenium you might have come across with challenges in identifying few objects of the web page due to application behavior. Hence scroll the page where you have the object or navigate the page to view the right element/object during execution time, this is one of the solutions if you see any challenges in identifying the page elements.
JavaScriptExecutor helps you to execute JavaScript through Selenium WebDriver with two methods. They are:
1. "executescript" - Executes JavaScript in the context of the currently selected frame or window in Selenium
2. "executeAsyncScript" - Your page renders more quickly
Here are few examples:
- Scroll down
- Scroll Up
- Scroll the page vertically
- Scroll to a particular element
- Scroll at a particular coordinate
- Scroll up in a vertical direction
- Scroll horizontally in the right direction
- Scroll horizontally in the left direction
- Using Robot Class – Scroll down
- Using Robot Class – Scroll Up
- Wait for a while
- Handle alerts
- Highlight an Element
- Get the page Title
- Page Refresh
- Open another URL
- Key in Value to TextBox
- Click Button
Scroll down:
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0,
document.body.scrollHeight);");
Scroll Up:
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0,
-document.body.scrollHeight);");
Scroll the page vertically:
JavascriptExecutor js = ((JavascriptExecutor) driver);
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor) driver).executeScript(“window.scrollTo(0,
document.body.scrollHeight)”);
Scroll to a particular element:
WebElement webelement = driver.findElement(By.xpath(“//input
[@name=webelement]”));
JavascriptExecutor js = ((JavascriptExecutor) driver);
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor)
driver).executeScript(“arguments[0].scrollIntoView();”,webelement);
Scroll at a particular coordinate:
JavascriptExecutor js = ((JavascriptExecutor) driver);
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor)
driver).executeScript(“window.scrollBy(400,500)”);
Scroll up in a vertical direction:
JavascriptExecutor js = ((JavascriptExecutor) driver);
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor)
driver).executeScript(“window.scrollTo(document.body.scrollHeight,0)”);
Scroll horizontally in the right direction:
JavascriptExecutor js = ((JavascriptExecutor) driver);
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor) driver).executeScript(“window.scrollBy(3000,0)”);
Scroll horizontally in the left direction:
JavascriptExecutor js = ((JavascriptExecutor) driver);
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor)driver).executeScript(“window.scrollBy(-3000,0)”);
Using Robot Class – Scroll down
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
Using Robot Class – Scroll Up
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_PAGE_UP);
robot.keyRelease(KeyEvent.VK_PAGE_UP);
Wait for 5 min
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");
Handle alerts
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("alert('Hello, Vasu Chiluveru');");
driver.switchTo().alert().accept();
Highlight an Element
WebElement uName = driver.findElement(By.name("UserName"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.border='6px dotted blue'", uName );
Get page Title
Wait for 5 min
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");
Handle alerts
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("alert('Hello, Vasu Chiluveru');");
driver.switchTo().alert().accept();
Highlight an Element
WebElement uName = driver.findElement(By.name("UserName"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.border='6px dotted blue'", uName );
Get page Title
JavascriptExecutor js =(JavascriptExecutor) driver; String pageTitle = js.executeScript("return document.title;").toString(); System.out.println("Page Title "+pageTitle); Page Refresh JavascriptExecutor js =(JavascriptExecutor) driver; js.executeScript("history.go(0);"); Open URL JavascriptExecutor js =(JavascriptExecutor) driver; js.executeScript("window.location = 'https://www.google.com'"); Enter value in to TextBox JavascriptExecutor js =(JavascriptExecutor) driver; js.executeScript("document.getElementById('userName').value='Vasu Chiluveru'"); Click Button JavascriptExecutor js =(JavascriptExecutor) driver; js.executeScript("document.querySelector('#button').click()");