Gmail login fail using Selenium webdriver. Showing element not found for password
Try setting an implicit wait of maybe 10 seconds.
gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Or set an explicit wait. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. In your case, it is the visibility of the password input field. (Thanks to ainlolcat's comment)
WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
gmail.findElement(By.id("Passwd")).sendKeys("xyz");
Explanation: The reason selenium can't find the element is because the id
of the password input field is initially Passwd-hidden
. After you click on the "Next" button, Google first verifies the email address entered and then shows the password input field (by changing the id from Passwd-hidden
to Passwd
). So, when the password field is still hidden (i.e. Google is still verifying the email id), your webdriver starts searching for the password input field with id Passwd
which is still hidden. And hence, an exception is thrown.
GMail is blocking login via Automation (Selenium)
After some trial and error, found out that this issue happens only in a scenario when multiple gmail accounts have already been created from the same App/IP/Device. Google somehow is marking those accounts and blocks them if they are launched by automation frameworks/extensions.
Temporary Solutions:
- Create a fresh GMail account using a
different mobile number from another device (Not recommended). - We should be using workarounds like nodemailer
zeolearn.com/magazine/sending-and-receiving-emails-using-nodejs
(as mentioned by Rahul L as a suggestion) - Automate temporary mail providers like Guerilla Mail or 10 Minute Mail if you are
worried about only receiving mails
My humble opinion is to entirely avoid automating the UI of third party Mail applications as you cannot predict how their UI and elements will change. They might block you from launching for security purposes and they have every right to do so!
Selenium Google Login Block
This error message...
...implies that the WebDriver instance was unable to authenticate the Browsing Context i.e. Browser session.
This browser or app may not be secure
This error can happen due to different factors as follows:
In the article "This browser or app may not be secure" error when trying to sign in with Google on desktop apps @Raphael Schaad mentioned that, if an user can log into the same app just fine with other Google accounts, then the problem must be with the particular account. In majority of the cases the possible reason is, this particular user account is configured with Two Factor Authentification.
In the article Less secure apps & your Google Account it is mentioned that, if an app or site doesn’t meet google-chrome's security standards, Google may block anyone who’s trying to sign in to your account from it. Less secure apps can make it easier for hackers to get in to your account, so blocking sign-ins from these apps helps keep your account safe.
Solution
In these cases the respective solution would be to:
- Disable Two Factor Authentification for this Google account and execute your @Test.
- Allow less secure apps
You can find a detailed discussion in Unable to sign into google with selenium automation because of "This browser or app may not be secure."
Deep Dive
However, to help protect your account, Web Browsers may not let you sign in from some browsers. Google might stop sign-ins from browsers that:
- Doesn't support JavaScript or have Javascript turned off.
- Have AutomationExtension or unsecure or unsupported extensions added.
- Use automation testing frameworks.
- Are embedded in a different application.
Solution
In these cases there are diverse solutions:
Use a browser that supports JavaScript:
Chrome
Safari
Firefox
Opera
Internet Explorer
Edge
Turn on JavaScript in Web Browsers: If you’re using a supported browser and still can’t sign in, you might need to turn on JavaScript.
If you still can’t sign in, it might be because you have AutomationExtension / unsecure / unsupported extensions turned on and you may need to turn off as follows:
public class browserAppDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
WebDriver driver = new ChromeDriver(options);
driver.get("https://accounts.google.com/signin")
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='identifierId']"))).sendKeys("gashu");
driver.findElement(By.id("identifierNext")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='password']"))).sendKeys("gashu");
driver.findElement(By.id("passwordNext")).click();
System.out.println(driver.getTitle());
}
}
- You can find a couple of relevant discussions in:
- Gmail login using selenium webdriver in java
- Selenium test scripts to login into google account through new ajax login form
Additional Considerations
Finally, some old browser versions might not be supported, so ensure that:
- JDK is upgraded to current levels JDK 8u241.
- Selenium is upgraded to current levels Version 3.141.59.
- ChromeDriver is updated to current ChromeDriver v80.0 level.
- Chrome is updated to current Chrome Version 80.0 level. (as per ChromeDriver v80.0 release notes)
Selenium Gmail login password field
This could be a combination of a wait issue and selecting the wrong element.
Try changing your selector in your sendKeys as follows:
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("********");
If that still doesn't work, you could try a different wait condition before that call, such as:
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password]")));
You might need to do some experiments to find the right wait condition, but doing both of those things should get you what you need. :)
Related Topics
Try-Catch-Finally-Return Clarification
Why Java.Util.Optional Is Not Serializable, How to Serialize the Object with Such Fields
How to Properly Express Jpql "Join Fetch" with "Where" Clause as JPA 2 Criteriaquery
How to Scale Threads According to CPU Cores
Create a Autocompleting Textbox in Java with a Dropdown List
Connecting an Input Stream to an Outputstream
Java Enum and Additional Class Files
Why Is an Instance Variable of the Superclass Not Overridden by a Subclass
Updating UI from Different Threads in Javafx
Is There an Equivalent of Java.Util.Regex for "Glob" Type Patterns
How to Add a New Line of Text to an Existing File in Java
Static Versus Non-Static Lock Object in Synchronized Block
Difference Between Java.Exe and Javaw.Exe
How to Do Union, Intersect, Difference and Reverse Data in Java
Drawing a Simple Line Graph in Java
Create a New Line in Java's Filewriter
Error: Servlet Jar Not Loaded... Offending Class: Javax/Servlet/Servlet.Class