Till yesterday the below mentioned code was working fine but now i am facing some problem .
The code opens firefox browser then loads facebook.com but the code is not sending
email and password to web browser i.e. sendkeys() is not working.
I verified the id of both textbox of email and password which are correct yet code is not working .
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class Webdriver2 {
WebDriver driver ;
JavascriptExecutor jse;
public void invokeBrowser()
{
try
{
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.19.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS );
driver.get("https://www.facebook.com/");
search();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void search()
{
try
{
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
Thread.sleep(4000);
driver.findElement(By.id("pass")).sendKeys("password");
Thread.sleep(4000);
driver.findElement(By.id("u_0_2")).click();
Thread.sleep(4000);
/*driver.findElement(By.name("q")).sendKeys("spit mumbai");
Thread.sleep(4000);
driver.findElement(By.xpath(" //button[#aria-label='Search' and #data-testid='facebar_search_button'] ")).click();*/
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Webdriver2 w = new Webdriver2();
w.invokeBrowser();
}
}
Try clicking on the two textboxes before you are performing the sendKeys:
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
The textboxes probably needs focus.
We need to take care of a couple of things here as follows:
The Email or Phone field is within an <input> tag so we need to take it into account while selecting the locator.
The Password field is also within an <input> tag so we need to take it into account while selecting the locator.
If you observe closely the id of the Log In button is dynamic and also within an <input> tag, so we need to consider this factor as well.
Here is the minimum sample code block using cssSelector to access the url https://www.facebook.com/, provide Email or Phone and Password, finally click on Log In button:
package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Facebook_Login_CSS_FF {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.cssSelector("input#email")).sendKeys("Ryusei");
driver.findElement(By.cssSelector("input[name='pass']")).sendKeys("Nakamura");
driver.findElement(By.cssSelector("input[value='Log In']")).click();
}
}
Related
What should happen is that these textfields for login should be filled and the the login button should be pressed (some sort of auto login)
Here is the link to the webpage: Telekom Email Login Page
This are the three methods I'm using:
public String exportDriver() throws IOException {
final InputStream IEDriverStream = getClass().getResourceAsStream("/Driver/IEDriverServer.exe");
final File ieDriverServer = FileWebOpener.stream2file(IEDriverStream, "IEDriverServer", ".exe");
System.setProperty("webdriver.ie.driver", ieDriverServer.getAbsolutePath());
return ieDriverServer.getAbsolutePath();
}
public void goToWebsite(String url) {
driver = new InternetExplorerDriver();
driver.get(url);
}
public void setUsernameAndPassword(String username, String password, int Website) throws InterruptedException {
try{ new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys(username);
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys(password);
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
} catch(Exception e){
e.printStackTrace();
}
}
This is the exception I get
I believe you first need to :
click on the username field.
send username keys.
click on the password field.
send password keys.
click on the submit button
try- findElement(By.Class("line_normalized clear relative").click);
this find the username textfield class and clicks on it if Im not mistaken, syntax may be wrong but the idea is the same.
same goes for the password field, find the class/id name of the field & click on it before sending keys.
I have tested it with C# and get the right behaviors.
Below is the code:
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System;
namespace IEWebDriver
{
class Program
{
static void Main(string[] args)
{
string username = args.Length > 1 ? args[1] : "test";
string password = args.Length > 1 ? args[2] : "test";
IWebDriver driver = new InternetExplorerDriver();
//IWebDriver driver = new ChromeDriver();
//Navigate to test URL
driver.Navigate().GoToUrl("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
try
{
new WebDriverWait(driver, new TimeSpan(0, 0, 20, 60)).Until(c => c.FindElement(By.CssSelector("input#user[name='pw_usr']"))).SendKeys(username);
driver.FindElement(By.CssSelector("input#pw_pwd[name='pw_pwd']")).SendKeys(password);
driver.FindElement(By.CssSelector("input.button.standard_button_size.large#pw_submit")).Click();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
//Close the browser
driver.Quit();
//driver.Close();
}
}
}
Hi as you want to enter the username and it's a input field, you need to locate the input tag.
you can try the following locator to locate the text input area and perform the sendkeys operation:
driver.findElement(By.xpath("//div[#class='line_normalized clear relative']/input")).sendKeys("username");
I suspect your application is too slow.Use WebDriverWait to identify the element and then Sendkeys operation.
package pkg1;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class testIE {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement user= wait.until(ExpectedConditions.elementToBeClickable(By.id("user")));
user.sendKeys("user12345#gmail.com");
WebDriverWait wait1 = new WebDriverWait(driver, 30);
WebElement pass= wait1.until(ExpectedConditions.elementToBeClickable(By.id("pw_pwd")));
pass.sendKeys("password");
}
}
Output
As you are accessing the website initially you need to induce WebDriverWait for the desired elements to be clickable and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
xpath:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='user' and #name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.xpath("//input[#id='pw_pwd' and #name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.xpath("//input[#class='button standard_button_size large' and #id='pw_submit']")).click();
I'm testing this on my local system and trying to verify user is successfully logged in and landed on correct page.
Getting error when trying to compare String data with webelement data.
`package wnsautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\orange\\Downloads\\geckodriver.exe");
driver= new FirefoxDriver();
WebDriverWait myWait = new WebDriverWait(driver, 10);
String baseUrl = "http://192.168.1.52:9000";
driver.get(baseUrl);
myWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")));
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")).sendKeys("admin#gmail.com");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[3]/div/div/input")).sendKeys("8JXzwRs4VWeGP0Sy");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[5]/button")).click();
String expectedtext="Summary";
WebElement actualtext;
actualtext = driver.findElement(By.linkText("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3"));
if (actualtext.contentEquals(expectedtext)){
System.out.println("User succesfully loggedIN");
} else {
System.out.println("Invalid credtendials!!");
}
}
}`
Instead of using Webelement, use String with getText()method so, it will give you the actual String. Not the Webelement.
Note: Instead of using absolute xpath, use relative xpath. For more details on xpath tutorial, refer this link.
String expectedtext="Summary";
String actualtext = driver.findElement(By.xpath("//div/h3[text()='Summary']")).getText();
System.out.println(actualtext);
if (expectedtext.equalsIgnoreCase(actualtext))
{
System.out.println("User succesfully loggedIN");
}
else
{
System.out.println("Invalid credtendials!!");
}
Try this-
String expectedtext="Summary";
String actualtext= null;
actualtext = driver.findElement(By.xpath("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3")).gettext();
I tried this exact code on other websites and it seems to work fine. It's just on pizza hut that it can't even locate an element let alone click on it. Thread.sleep() doesn't make a difference. The problem is between the commented *, according to the compiler. Here's the code.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
//************************************************
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
I just ran the code below and it worked just fine. No need for sleep or waits and I'm using Chrome also.
driver.get("https://www.pizzahut.com");
driver.findElement(By.id("lg-nav-pizza")).click();
Try this one ,definitely it help you.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
// System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
List<WebElement> webElements=driver.findElement(By.cssSelector("div.btn-group.btn-group-lg.btn-group-justified.ph-header-navigation"))
.findElements(By.cssSelector("div.btn.btn-link.ph-ghost-padding.ng-scope"));
for (WebElement element : webElements) {
if (element.getAttribute("title").equals("PIZZA")) {
element.click();
}
}
Thread.sleep(5000);
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
You are not able to click it because it is an AngularJS tag, hence it takes some time to load: add some wait like below and it works (Here i'm just adding thread.sleep for the sake of simplicity,I've increased it to 15000ms):
//CLICK PIZZA
Thread.sleep(15000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
I have written a program to click on a link, enter the username and Password and then click on the Signin Button, but i'am not getting the output and its giving me an error.
The Program is as below
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CssSelector3 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id='yui_3_12_0_1_1454585688215_223']/div/ul[1]/li[1]/a")).click();
driver.findElement(By.cssSelector("#login-username")).sendKeys("tester#yahoo.com");
driver.findElement(By.cssSelector("#login-passwd")).sendKeys("tester1234");
driver.findElement(By.cssSelector("#login-signin")).click();
}
}
The program executes only till the maximize window part and then it stops. Can anyone assist me on this.
In your situation xpath of Sign In button is wrong. Yahoo generates everytime new id for its Sign In button. You can try following code.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[text()='Sign In']"))
.click();
driver.findElement(By.cssSelector("#login-username"))
.sendKeys("tester#yahoo.com");
driver.findElement(By.cssSelector("#login-passwd")).sendKeys("tester1234");
driver.findElement(By.cssSelector("#login-signin")).click();
}
The button pressing command doesn't work. It's finding the button, but isn't clicking the button. When clicking the button there should be a native page that opens within Gmail.
All the code below is attempting to click the button within the new contacts page of Gmail https://mail.google.com/mail/u/0/1#contact/new
Inspecting the element the div tag is div tabindex="0" aria-label="Email" data-tooltip="Email" aria-disabled="false" style="-moz-user-select: none;" id=":2l" class="T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3" role="button">div class="J-J5-Ji T-I-J3 Nz NS">/div>/div>
System.out.println("Finding Button");
driver.findElement(By.id(":2l")).click();
System.out.println("printing button");
System.out.println(driver.findElement(By.id(":2l")));
System.out.println("Finding button 2");
WebElement composeBtn = driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3']"));
System.out.println("Clicking button 2");
composeBtn.click();
System.out.println("Button 2 Clicked");
System.out.println(composeBtn.toString());
System.out.println("Finding button 3");
WebElement cBtn = driver.findElement(By.cssSelector("div[class= 'J-J5-Ji T-I-J3 Nz NS']"));
System.out.println("Clicking button 3");
cBtn.click();
Please let me know if you can help me identify this button
When I look at that page in the link you have provided, the compose button is grayed out and is not clickable. Having a program trying to click a button that a user could not click is still going to fail. Selenium will not and can not interact with objects that a user could not interact with (such as hidden fields, and in this case, grayed out buttons).
This method uses contains.
package testCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GmailFileUpload
{
WebDriver driver = null;
WebElement element = null;
#Before
public void setUp() throws Exception
{
File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#Test
public void test() throws InterruptedException, AWTException
{
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("Email")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.linkText("Gmail")).click();
Thread.sleep(5000);
//click on compose
//driver.findElement(By.xpath("//div[#class='T-I J-J5-Ji T-I-KE L3'] ")).click();
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#name='to']")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.xpath("//input[#name='subjectbox']")).sendKeys("aavinashpande#gmail.com");
Thread.sleep(5000);
element = driver.findElement(By.xpath("//div[#class='Ar Au']//div"));
element.click();
element.sendKeys("Hi Avinash");
Thread.sleep(3000);
}
#After
public void teardown() throws Exception
{
driver.quit();
}
}
I find the send button like this:
driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();
After that you can do a quit. Just have an extra pop up ask you to confirm leaving the account:
driver.Navigate().GoToUrl("https://mail.google.com/mail/logout?hl=en");
I have sent an Emil successfully through selenium automation using Gmail Account with the below script.
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.co.in/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#id=':jb']/div[#class='z0']/div")).click(); // Compose
selenium.type("//div[#class='wO nr l1']//textarea[#name='to']", "vikramn#gmail.com"); // For To
selenium.type("//div[#class='aoD az6']//input[#name='subjectbox']", "Wanted to SAY HI"); // For Subject
selenium.type("//div[#class='Ar Au']/div[#class='Am Al editable LW-avf']", "Hi Vikram");// For Message body
selenium.click("//div[#class='J-J5-Ji']/div[#class='T-I J-J5-Ji aoO T-I-atl L3']"); //send
You can use this code to compose email using selenium web driver for gmail
public void gmail() {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#aria-label='Email or phone']")).sendKeys("Your email");
driver.findElement(By.xpath("//span[.='Next']")).click();
//wait.until(ExpectedConditions.elementToBeClickable(password));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.xpath("//input[#aria-label='Enter your password']")).sendKeys("your password");
driver.findElement(By.xpath("//span[.='Next']")).click();
driver.findElement(By.xpath("//div[contains(text(),'Compose')]")).click();
}
driver.findElement(By.xpath("//*[#role='button' and text()='Compose']")).click();