0

我是一名学习者,我正在尝试从新的 gmail 撰写邮件,但在撰写窗口打开后无法将 id 输入到列表中,尝试切换框架方法但未能成功定位到字段本身,你能请帮助我。

谢谢。

4

3 回答 3

0

这是从 GMail 帐户撰写邮件的示例。

package testCase;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;

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("your gmail password");
        driver.findElement(By.id("signIn")).click();
        driver.findElement(By.linkText("Gmail")).click();
        driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click(); //Click on Compose button
        Thread.sleep(5000);
        driver.findElement(By.xpath("//textarea[@name='to']")).sendKeys("aavinashpande@gmail.com"); // write mail id to whom do you want to send an email
        driver.findElement(By.xpath("//input[@name='subjectbox']")).sendKeys("want to say Hello"); // write subject
        element = driver.findElement(By.xpath("//div[@class='Ar Au']//div"));
        element.click();
        element.sendKeys("Hi Avinash"); //type in message body
        driver.findElement(By.xpath("//div[contains(text(),'Send')]")).click(); //click on send button
    }
}
于 2015-04-08T08:42:22.487 回答
0

这个博客包含很多这样做的例子:

http://shilpamynampati.blogspot.com/

这是你需要的吗?

于 2013-11-04T15:12:47.417 回答
0
package basic;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class mozilaProj {

    public static void main(String[] args) throws InterruptedException {
        String path = "D:\\harsh\\Selenium data\\geckodriver.exe";  //location where your driver is placed.
            System.setProperty("webdriver.gecko.driver", path);  
        WebDriver wd=new FirefoxDriver();
        wd.navigate().to("http://accounts.google.com");

        wd.findElement(By.id("identifierId")).sendKeys("Your mail ID");
        wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/content/span")).click();  // Click on next 
        Thread.sleep(4000);  //wait needed here To get the password page.
        wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div/div[1]/div/div[1]/input")).sendKeys("Email Password");  // click on next
        wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/content")).click();  //If your page redirect to google security page.
        Thread.sleep(2000);
        wd.findElement(By.cssSelector(".gb_mf > path:nth-child(1)")).click(); //click on google apps tab. 
        wd.findElement(By.cssSelector("#gb23 > span:nth-child(5)")).click();  //click on gmail icon.
        wd.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div/div[1]/div/div")).click();   // click on compose  

         WebDriverWait wait = new WebDriverWait(wd, 20);   // implement wait here to load all data.
        wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.name("to")));
        wd.findElement(By.name("to")).sendKeys("recepient mail ID");
        wd.findElement(By.id(":mx")).sendKeys("Subject of mail");
        wd.findElement(By.id(":o0")).sendKeys("Mail body "); // type mail body 
        wd.findElement(By.id(":mn")).click();    // Send button click
        Thread.sleep(8000);  // Apply wait for sending mail 
        wd.close();

    }

}
于 2019-01-14T16:20:54.423 回答