Gmail login fail using Selenium webdriver. Showing element not found for password.

SeleniumSoftware TestingTesting Tools

We can encounter Gmail login failure while using Selenium webdriver due to the error - element not found for password. This can be fixed by the methods listed below −

  • Adding implicit wait - Implicit wait is applied to instruct the webdriver for polling the DOM(Document Object Model) for a specific amount of time while attempting to identify an element that is currently unavailable.

    The default value of the implicit wait time is 0. Once a wait time is set, it remains applicable throughout the entire life of the webdriver object. If an implicit wait is not set and an element is still not present in DOM, an exception is thrown.

Syntax

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

              Here, a wait time of five seconds is applied to the webdriver object.

  • Adding explicit wait - Explicit wait is applied to instruct the webdriver to wait for a specific condition before moving to the other steps in the automation script.

    The explicit wait is implemented using the WebDriverWait class along with expected_conditions. The expected_conditions class has a group of prebuilt conditions to be used along with the WebDriverWait class.

    Here, we can add the expected condition visibilityOfElementLocated for the element not found error for the password field in Gmail.

Example

Code Implementation

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FirstAssign{
   public static void main(String[] args){
      System.setProperty("webdriver.chrome.driver", "chromedriver");
      WebDriver driver = new ChromeDriver();
      try{
         //implicit wait of 15 seconds
         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
         //application launch
         driver.get("http://www.gmail.com");
         WebElement e = driver.findElement
         (By.xpath("//input[@type = 'email']"));
         e.sendKeys("abc@gmail.com");
         WebElement n = driver.findElement
         (By.xpath("//button[@type = 'button']"));
         n.click();

         //explicit wait
         WebDriverWait wait = new WebDriverWait(driver, 10);
         WebElement m = wait.until(
         ExpectedConditions.visibilityOfElementLocated
         (By.xpath("//input[@type = 'email']")));
         driver.findElement(By.xpath
         ("//input[@type = 'email']")).sendKeys("1234");
         n.click();
      }
      catch (Exception e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}
raja
Published on 18-Nov-2021 11:33:01
Advertisements