0

I am trying to write a test: go to gmail.com and login into account. And I have a trouble. Here is my code

driver.get(gmailUrl);
    WebElement elLogin = driver.findElement(By.cssSelector("input#identifierId"));
    elLogin.sendKeys(gmailLogin);
    WebElement elClick = driver.findElement(By.xpath("//content/span[contains(text(),'Next')]"));
    elClick.click();

    WebElement elPass =      driver.findElement(By.cssSelector("input.whsOnd.zHQkBf"));
    //String pass = elPass.getTagName();
    //System.out.println(pass);
    //WebDriverWait wait = new WebDriverWait(driver, 5);
    Thread.sleep(2000);
    elPass.sendKeys(gmailPassword);

I got error message "element is not attached to the page document" If anybody could run my code and tell me where is my mistake, I would be very appreciate.

  • Possible duplicate of [Selenium test scripts to login into google account through new ajax login form](https://stackoverflow.com/questions/45953043/selenium-test-scripts-to-login-into-google-account-through-new-ajax-login-form) – undetected Selenium Mar 30 '18 at 12:22
  • Try with this xpath "//input[@name='password']" – santhosh kumar Mar 30 '18 at 12:52
  • I assume that you don't actually have to test gmail, but rather do an exercise ☺. If you're allowed, then prefer to use the html only version of gmail as it's much easier to automate with Selenium. See https://support.google.com/mail/answer/15049?hl=en – Arnon Axelrod Apr 01 '18 at 05:22

2 Answers2

0

I think your selection for the password input element is not good. If I open gmail.com the classes assigned to this element are "rFrNMe KSczvd BlbNGe zKHdkd sdJrJc Tyc9J". It seems the classes are created dynamically by whatever google uses as backend framework for this site.

So you should find a better CSS selector that identifies the password element. I don't have a Java environment here, so I can only hint (no guarantee of succes):

    WebElement elPass = driver.findElement(
         By.cssSelector("#password input"));

This selects the first input element within the element with id = password. The space in the CSS selector means to find the input element below the element with with the given id. You could also write "div[id=password] input", which is a tad bit more specific, since you now request the element with id = password to be of type div.

Look into https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors

luksch
  • 11,497
  • 6
  • 38
  • 53
  • Yeah I know it would be the best solution, but this input element doesn't have any ID ... What should I do? – WEBSITE FREELANCE Mar 30 '18 at 12:34
  • look at the css. it selects the ```div``` with name input first and then drills down to the ```input``` element within this div. CSS selectors can be quite complex. I suggest a read into it. for example here: https://saucelabs.com/resources/articles/selenium-tips-css-selectors – luksch Mar 30 '18 at 13:00
0

There is nothing wrong with your locators, I managed to login with them after a small change in your code.

"Element is not attached to the Page Document" also known as StaleElementReference exception means that although the element is found it's no longer attached to the DOM at the moment you want to interact with it. The main reason for this is a (partly) refresh of the page.

When you move the Thread.Sleep one line back, so before the line with "WebElement elPass = ....." , your code will wait till that part of the page is refreshed before searching for the password field and Selenium will have no problems anymore to interact with this password field.

Find here a code solution I tested with succes :

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Question49574195
{
    public static void main(String[] args) throws InterruptedException 
  {
    System.setProperty("webdriver.chrome.driver", "c:/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://mail.google.com/mail");
    WebElement elLogin = driver.findElement(By.cssSelector("input#identifierId"));
    elLogin.sendKeys("youremailadress");
    WebElement elClick = driver.findElement(By.xpath("//content/span[contains(text(),'Next')]"));
    elClick.click();
    Thread.sleep(3000);
    WebElement elPass = (driver.findElement(By.cssSelector("input.whsOnd.zHQkBf")));
    elPass.sendKeys("yourpassword");
    WebDriverWait wait = new WebDriverWait(driver, 15);
    WebElement next = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//content/span[contains(text(),'Next')]")));
    next.click();
}}
Frank
  • 831
  • 1
  • 11
  • 23