Java 使用SeleniumWebDriver在Gmail中单击撰写按钮

Java 使用SeleniumWebDriver在Gmail中单击撰写按钮,java,html,css,selenium,webdriver,Java,Html,Css,Selenium,Webdriver,按下按钮的命令不起作用。它正在查找按钮,但没有单击按钮。当点击按钮时,应该会有一个本地页面在Gmail中打开 下面的所有代码都试图单击Gmail的“新建联系人”页面中的按钮 检查div标记为div的元素tabindex=0 aria label=Email data tooltip=Email aria disabled=false style=-moz用户选择:无;id=:2l class=T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3角色=按钮>div class=J-J5

按下按钮的命令不起作用。它正在查找按钮,但没有单击按钮。当点击按钮时,应该会有一个本地页面在Gmail中打开

下面的所有代码都试图单击Gmail的“新建联系人”页面中的按钮

检查div标记为div的元素tabindex=0 aria label=Email data tooltip=Email aria disabled=false style=-moz用户选择:无;id=:2l class=T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3角色=按钮>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();

请告诉我您是否可以帮助我识别此按钮

当我在您提供的链接中查看该页面时,“撰写”按钮变灰,无法单击。让程序尝试单击用户无法单击的按钮仍然会失败。Selenium不会也不能与用户无法与之交互的对象交互,例如隐藏字段,在本例中为灰显按钮。

我已通过Selenium automation使用Gmail帐户通过以下脚本成功发送了EML

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

此方法使用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();
    }
}

我发现发送按钮如下所示:

driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();
之后你可以退出。只需额外弹出一个窗口,要求您确认是否离开帐户:

driver.Navigate().GoToUrl("https://mail.google.com/mail/logout?hl=en");

您可以使用此代码使用SeleniumWebDriverforGmail撰写电子邮件

公开作废gmail{ driver.manage.timeouts.implicitlyWait20,TimeUnit.SECONDS; driver.findElementBy.xpath//input[@aria label='Email或phone']; driver.findElementBy.xpath//span[.='Next']。单击; //wait.untelexpectedconditions.element要取消选中密码; 试一试{ Thread.sleep3000; }捕捉中断异常e{ //TODO自动生成的捕捉块 e、 打印跟踪; } driver.findElementBy.xpath//input[@aria label='Enter your password']; driver.findElementBy.xpath//span[.='Next']。单击; driver.findElementBy.xpath//div[containstext,'Compose']; }
对不起,我想我应该提到,您需要添加一个新联系人才能使按钮不再变灰。然后,当您按下按钮时,我正在解释新联系人将已添加到您发送的电子邮件中,您只需更新邮件的主题和正文即可发送。您可以使用上述代码使用selenium web driver for Gmail撰写邮件谢谢!我注意到驱动程序来自FireFox而不是Chrome。很高兴它成功了
driver.findElement(By.xpath("//*[@role='button' and text()='Compose']")).click();