Skip to content

Instantly share code, notes, and snippets.

@jaysonrowe
Created November 12, 2012 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaysonrowe/4057305 to your computer and use it in GitHub Desktop.
Save jaysonrowe/4057305 to your computer and use it in GitHub Desktop.
Checking Gmail with Selenium Webdriver in C#
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace TestingSeleniumGmail
{
[TestClass]
public class FillFormIntegrationTest
{
public static string BaseUrl = "http://www.gmail.com";
public const int TimeOut = 30;
[TestMethod]
public void Login()
{
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl(BaseUrl);
var loginBox = driver.FindElement(By.Id("Email"));
loginBox.SendKeys("email.address@gmail.com");
var pwBox = driver.FindElement(By.Id("Passwd"));
pwBox.SendKeys("!SuperSecretpassw0rd");
var signinBtn = driver.FindElement(By.Id("signIn"));
signinBtn.Click();
driver.Quit();
}
}
}
@igenyar
Copy link

igenyar commented Apr 20, 2017

doesn't work (at least for now), unable to locate "#Passwd" element

@Anjali1221
Copy link

//To select sign in with different account

        var button= driver.FindElement(By.Id("account-chooser-link"));
        button.Click();

        // To click add account
        var button1 = driver.FindElement(By.ClassName("tXIHtc"));
        button1.Click();

        // To enter the email id
        var Login = driver.FindElement(By.Id("Email"));
        Login.SendKeys("sanjali1221@gmail.com");
        
        //To click next
         var move = driver.FindElement(By.Id("next"));
         move.Click();

        //To enter the password
        var password= driver.FindElement(By.Id("Passwd"));
        password.SendKeys("9840618730");            
       
        //To click sign in
        var button2 = driver.FindElement(By.Id("signIn"));
        button2.Click();

        //To click sign out
        var button3 = driver.FindElement(By.Id("gb_71"));
        button3.Click();

@IJsWorkshop
Copy link

I know this is an old post but i came across this when i was googling and decided to leave this for the next generation of peeps searching for why the password cant be found 👍

I've got this to work successfully with chromium. You need to modify the wait function in the driver as it doesnt work but ill add some code so you can use this instead.

Add this code to a static class

public static bool IsVisible(this IWebDriver webDriver, By locator, TimeSpan timeOut) {
  bool isDisplayed = false;
  var sw = Stopwatch.StartNew();
  sw.Start();
  while (!isDisplayed || sw.Elapsed.Seconds <= timeOut.Seconds) 
  {
      IWebElement ele = null;
      try {
          ele = webDriver.FindElement(locator);
      }
      catch (Exception) {
          //Debug.WriteLine($"timeout :{sw.Elapsed.Seconds} : {e.Message}");
      }
  
      if (ele != null && ele.Displayed)  {
          isDisplayed = true; 
          sw.Stop();
          return true;
      }
      Delay(TimeSpan.FromSeconds(1)); // you can remove this
  }
  sw.Stop();
  return true;
}

How to Use

string ChromeDriverPath = "this is the local address of your chromium browser";

var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--start-maximized");

var driver = new ChromeDriver(ChromeDriverPath, chromeOptions);

// navigate to Gmail login page
driver.Navigate().GoToUrl("https://accounts.google.com/signin"); 

// find and fill in the email field
driver.FindElement(By.Id("identifierId")).SendKeys("your_email@gmail.com"); 

// click the next button
driver.FindElement(By.Id("identifierNext")).Click();

// repeat attempt to detect element or a maximum of 7 seconds has expired
driver.IsVisible(By.XPath("//*[@id='password']/div/div/div/input"), TimeSpan.FromSeconds(7));

// send information to element and add keystroke
var pwd = driver.FindElement(By.XPath("//*[@id='password']/div/div/div/input"));
pwd.SendKeys("@SuperSecretPassword@");

// find next button and click
driver.FindElement(By.Id("passwordNext")).Click();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment