使用Selenium WebDriver点击Gmail中的Compose按钮



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

下面的所有代码都试图点击Gmail新联系人页面中的按钮https://mail.google.com/mail/u/0/1#contact/new

检查元素时,div标记为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();

请让我知道你是否可以帮助我识别这个按钮

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

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

我使用以下脚本通过selenium自动化成功发送了一个Emil。

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

您可以使用此代码为gmail 使用selenium web驱动程序编写电子邮件

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();

最新更新