Facing issues with Selnium Webdriver and xpaths:

19 views
Skip to first unread message

Bhoomika

unread,
Jul 13, 2011, 1:21:47 PM7/13/11
to SeleniumWiki
Hi,

I am trying to write a script to Send email through Gmail using Web
driver.

I am having a tough time with xpaths and need some help on how xpaths
should be specified when using Selenium2. Please see below the code I
have written. The code may not be perfect as I am totally new to
selenium and trying my hands for the first time at this. Any help
would be greatly appreciated.

Code:

package com.selenium.scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class gmail {
static WebDriver driver;
static Wait wait;
private static final String USER_NAME = “xxx″;
private static final String PASSWORD = “xxx″;
private static final String EMAIL = “xxx”;
private static final String SUBJECT = “Subject & Body only mail”;
private static final String MAIL_BODY = “Hi, How r u doing”;

public static void main(String[] args) {
driver = new FirefoxDriver();

wait = new WebDriverWait(driver, 30);
driver.get(“https://www.google.com/accounts/ServiceLogin?
service=mail&passive=true&rm=false&continue=http%3A%2F
%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy
%3Dl&bsv=llya694le36z&scc=1&ltmpl=default&ltmplcache=2&from=login”);
driver.switchTo().defaultContent();
boolean result;
try {
result = Signin();
} catch(Exception e) {
e.printStackTrace();
result = false;
} finally {
driver.close();
}
System.out.println(“Test ” + (result? “passed.” : “failed.”));
if (!result) {
System.exit(1);
}

}
private static boolean Signin() {

//type search query
driver.findElement(By.name(“Email”)).sendKeys(USER_NAME);
// click search
driver.findElement(By.name(“Passwd”)).sendKeys(PASSWORD);
// Wait for search to complete
//driver.findElement(By.name(“signIn”)).click();
driver.findElement(By.name(“signIn”)).click();
driver.switchTo().defaultContent();

wait.until(new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {
// webDriver.switchTo().defaultContent();
webDriver.getPageSource().contains(“Compose mail”);
return Boolean.valueOf(true);

}
});

//wait.until(driver.getPageSource().contains(“Compose mail”));
if(driver.getPageSource().contains(“Compose mail”))
{ System.out.println(“Successful login to Gmail”); }

//Clicking on Compose mail button

driver.switchTo().defaultContent();
driver.switchTo().frame(“canvas_frame”);
WebElement element = driver.findElement(By.xpath(“/html/body/div[1]/
div[2]/div/div[2]/div[1]/div[1]/div[1]/div[3]/div[@id=':k5']/div[1]/
div[@id=':k6']/div/div”));
element.click();

//driver.switchTo().defaultContent();

wait.until(new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {
webDriver.getPageSource().contains(“Discard”);
return Boolean.valueOf(true);

}
});

driver.switchTo().defaultContent();
driver.switchTo().frame(“canvas_frame”);

System.out.println(“passed compose button”);
driver.switchTo().defaultContent();
//Entering the destination email address in “To” field
WebElement element1 = driver.findElement(By.id(“:g8″));
element1.sendKeys(EMAIL);

//Entering the subject in “Subject” field
driver.findElement(By.xpath(“//*[@id=':g5']“)).sendKeys(SUBJECT);

//Entering email content
driver.findElement(By.xpath(“//html/body[@class='editable LW-
yrriRe']“)).sendKeys(MAIL_BODY);

//Sending the email by clicking on Send button
driver.findElement(By.xpath(“//div[@role='button']/b”)).click();

//Look for mail sent message
return driver.findElement(By.tagName(“body”)).getText().contains(“Your
message has been sent”);
}

}

Pavandeep Puddupakkam

unread,
Jul 14, 2011, 7:05:25 AM7/14/11
to SeleniumWiki
To get to the compose link button you can use "//div[@class]
[.='Compose mail']" instead of using “/html/body/div[1]/div[2]/div/
div[2]/div[1]/div[1]/div[1]/div[3]/div[@id=':k5']/div[1]/
div[@id=':k6']/div/div”

for email content you can use //body[@class='editable LW-yrriRe']
instead of “//html/body[@class='editable LW-yrriRe']“

For more information on xpath you can refer to
http://www.seleniumwiki.com/category/automation-tips/xpath-automation-tips/.

If you have any questions feel free to post it on this group or on
SeleniumWiki.com.

Bhoomika

unread,
Jul 14, 2011, 10:33:57 AM7/14/11
to SeleniumWiki
Hi,

The xpath //body[@class='editable LW-yrriRe'] is not working.

Thanks
Bhoomika

On Jul 14, 7:05 am, Pavandeep Puddupakkam <puddupak...@gmail.com>
wrote:
> To get to the compose link button you can use "//div[@class]
> [.='Compose mail']" instead of using “/html/body/div[1]/div[2]/div/
> div[2]/div[1]/div[1]/div[1]/div[3]/div[@id=':k5']/div[1]/
> div[@id=':k6']/div/div”
>
> for email content you can use //body[@class='editable  LW-yrriRe']
> instead of “//html/body[@class='editable LW-yrriRe']“
>
> For more information on xpath you can refer tohttp://www.seleniumwiki.com/category/automation-tips/xpath-automation....
Reply all
Reply to author
Forward
0 new messages