2

I am trying to write a program for Gmail login using Selenium. I am able to hit URL, enter username and click "Next" button, but when I redirected to "Password" field page I am unable to locate that element. I tried the same using by.className. Following is the inspected code for field:

<input class="whsOnd zHQkBf" 
       jsname="YPqjbf"  
       autocomplete="current-password"  
       spellcheck="false"  
       tabindex="0"  
       aria-label="Enter your password"  
       name="password"  
       autocapitalize="off"  
       autocorrect="off"  
       dir="ltr"  
       data-initial-dir="ltr"  
       data-initial-value=""  
       badinput="false"  
       type="password">

and following is selenium code I have written:

driver.findElement(By.className("whsOnd zHQkBf")).sendKeys("Password");

I also tried using Id, by using parent <div id ="password"....>, but it didn't work.

Please let me know the solution.

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
Darshana
  • 105
  • 1
  • 3
  • 8
  • please try my answer and let me know your feedback – Mahmud Riad Jan 04 '18 at 14:13
  • Possible duplicate of [automation Gmail login using selenium webdriver in java](https://stackoverflow.com/questions/44455269/automation-gmail-login-using-selenium-webdriver-in-java) – undetected Selenium Jan 04 '18 at 15:24
  • What does "didn't work" mean? Please post the relevant error messages for each attempt or describe in what way the code attempt didn't work. – JeffC Jan 04 '18 at 16:13
  • The error message for the `className()` attempt is going to be that you can't use compound classes. You can't use `.className()` to find more than one class and you've provided two, note the space between the class names. – JeffC Jan 04 '18 at 16:14
  • I think google changed something with the way their login works recently, and it's causing this problem. I tried using a piece of selenium code that's always worked in the past, but it's giving errors every time now, no matter what I try. Most of the errors are either timeouts, or some variant of "Element not visible." – Drigan Jan 10 '18 at 03:52

7 Answers7

2

whsOnd zHQkBf means that the element has two CSS classes whsOnd and zHQkB. So you won't find anything that has a class with the name whsOnd zHQkBf.

You have to instead use a CSS selector to find an element with multiple classes like this: .whsOnd.zHQkBf. But this matches multiple elements on the google login page. It's easier to use the following CSS selector input[type='password'].

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
Cyril
  • 2,376
  • 16
  • 21
1

use the following code

driver.findElement(By.cssSelector("[name=\"password\"]")).sendKeys("Password");
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
Andreas
  • 96
  • 4
1

Please use the below code

 driver.findElement(By.cssSelector("input[type='password']")).sendKeys("Password");
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
1

The name locator would be the simplest :

driver.findElement(By.name("password")).sendKeys("Password");
HaC
  • 902
  • 12
  • 24
  • Sorry, but none of the above is working. Please give me some other solution. – Darshana Jan 08 '18 at 07:25
  • Please elaborate on what's not working - was there an error? – HaC Jan 09 '18 at 03:09
  • This was not working for me earlier. I was going to try it to give you my error message, but it's now worked three times in a row. The only thing I did differently this time vs. previously is I included a 2 second sleep just before this call. – Drigan Jan 10 '18 at 03:56
1

This might help you:

driver.findElement(By.xpath(".//*[@id='password']//input[@name='password']")).sendKeys("Password");
hiren
  • 1,067
  • 1
  • 9
  • 16
  • This just gave me a timeout exception. It's like it finds the element, clicks it, but needs to take a second for some js to run before it fills in the password. – Drigan Jan 10 '18 at 03:13
  • You might want to Javascript insert on that element. – hiren Jan 10 '18 at 04:18
  • I was able to get all the other answers to work except this one just by adding a 2 second wait before it. Regardless, it was playing with this answer that made me realize what the problem was for me. Maybe the OP has a different problem. – Drigan Jan 10 '18 at 04:23
  • Sorry. Missed an opening bracket (opening square bracket before @id). Edited. – hiren Jan 10 '18 at 08:44
0

driver.findElement(By.className("whsOnd zHQkBf")).sendKeys("Password");

Hi buddy,

The statement which you have written is wrong class name because its a compound class and we cannot use compound class to locate an element. The username and password fields are developed using angular-JS so when click sign in url both username and password will be downloaded but for user only username is shown so the password is hidden background

Try this code this will work for sure because i have tried already

driver.findElement(By.xpath("//[@id='password']/child::[1]/child::/child::/child::*")).sendKeys("Password");

Hope you got answer Happy Testing

shankar
  • 15
  • 4
  • Don't use compound class to locate any item use single class name from your statement className should be "whsOnd" not "whsOnd zHQkBf" – shankar Jan 12 '18 at 15:26
0

Below code worked for me. Some time element will not be clickable so make driver to wait for some expected events to happen like clickable.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password));
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("your password");
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
  • I think there is some typo in the second line, it should be: wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password]")); – Rafael VC Jul 04 '21 at 07:30