Thursday, 7 December 2017

Selenium Tips and Tricks

Get the DOM Object into View:

At times, few actions are not performed on a DOM object until it is visible in the view-port. Below is the small code snippet to get the DOM object into view using WebDriver's 'JavascriptExecutor' interface. 

WebElement element = driver.findElement("someId"));
JavascriptExecutor js= (JavascriptExecutor) driver;


((JavascriptExecutor) driver).executeScript("document.getElementById('text123').scrollIntoView(true);");

Selenium WebDriver Wait Commands:

For any automation scripts to work without any sync issues with application, wait statements are very important. Selenium WebDriver provide different types of Wait conditions to make automation scripts effective.

Below are different types of waits & its usage.

  • Implicit Wait: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element. The default setting is 0.
    NOTE: Implicit wait is set for the life of the WebDriver instance.

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

  • Explicit Wait: An explicit wait is wait time explicitly defined to wait for a certain condition to occur before proceeding to the next step. Setting Explicit wait using 'WebDriverWait' is an effective way to handle certain expected conditions.
WebDriverWait wait = new WebDriverWait(driver, 10);

Other Expected Conditions:

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id= 'TestId'"), "Sample Text"));
wait.until(ExpectedConditions.textToBePresentInElementValue(By.xpath("//div[@id= 'TestId'"), "Sample Text"));
wait.until(ExpectedConditions.titleContains("Sample Title"));
wait.until(ExpectedConditions.invisibilityOfElementWithText("someid"), "txt"));

Expected Conditions with Logical Operators:

// When both conditions returns true

// When either of the condition returns true

Wait for Ajax Calls to Complete:

public boolean waitForAjaxCalls(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 30);

ExpectedCondition<Boolean> jQueryWait = new ExpectedCondition<Boolean>() {
  public Boolean apply(WebDriver driver) {
try {
  return ((Long)((JavascriptExecutor)driver).executeScript("return") == 0);
catch (Exception e) {
  return true;

ExpectedCondition<Boolean> jsWait = new ExpectedCondition<Boolean>() {
  public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").toString().equals("complete");

return wait.until(jQueryWait) && wait.until(jsWait);

Set Wait Timeouts:

driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(100, TimeUnit.SECONDS);

Handle Split Button Click:

Normally split buttons are put in "<em>" tag & getting the locator working  is tricky.
Long xpaths might not work with "<em>" tag, even though you find that correct in FirePath or any xpath vaidator. This would not work during execution.

As per my experience with split button automation, it is good to go with shorter xpath containing <em> tag's className or id.

Below example to make split button work:

WebDriver driver = new ChromeDriver();
driver.get("https://any url/");

// To handle Split Button


1 comment:

  1. Thanks for providing the information . The articles in your blog helped me a lot for improving the knowledge on the subject. Also check my small collection on this at selenium Online Training Bangalore blog