LayoutTitle

Hoverable Dropdown

Thursday, June 13, 2019

JavascriptExecutor - Selenium WebDriver


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:
  1. Scroll down
  2. Scroll Up
  3. Scroll the page vertically
  4. Scroll to a particular element
  5. Scroll at a particular coordinate
  6. Scroll up in a vertical direction
  7. Scroll horizontally in the right direction
  8. Scroll horizontally in the left direction
  9. Using Robot Class – Scroll down
  10. Using Robot Class – Scroll Up
  11. Wait for a while
  12. Handle alerts
  13. Highlight an Element
  14. Get the page Title
  15. Page Refresh
  16. Open another URL
  17. Key in Value to TextBox
  18. 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) 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) driver).executeScript(“arguments[0].scrollIntoView();”,webelement);

Scroll at a particular coordinate:
JavascriptExecutor js = ((JavascriptExecutor) driver);
 ((JavascriptExecutor) driver).executeScript(“window.scrollBy(400,500)”);

Scroll up in a vertical direction:
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor) driver).executeScript(“window.scrollTo(document.body.scrollHeight,0)”);

Scroll horizontally in the right direction:
JavascriptExecutor js = ((JavascriptExecutor) driver);
((JavascriptExecutor) driver).executeScript(“window.scrollBy(3000,0)”);

Scroll horizontally in the left direction:
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
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()");