使用Selenium WebDriver点击Gmail中的撰写按钮

时间:2014-10-15 19:34:30

标签: java html css selenium webdriver

按下按钮命令不起作用。它找到了按钮,但没有点击按钮。点击该按钮后,应该会在Gmail中打开一个原生页面。

以下所有代码都是在点击Gmail https://mail.google.com/mail/u/0/1#contact/new

的新联系人页面中的按钮

检查元素div标签是div tabindex =" 0"咏叹调标签="电子邮件"数据提示="电子邮件"咏叹调禁用="假" 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();

如果您能帮我识别此按钮,请告诉我

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我找到了这样的发送按钮:

driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();

之后你可以退出。只需要一个额外的弹出窗口,请您确认离开帐户:

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

答案 3 :(得分:0)

我已使用以下脚本使用Gmail帐户通过selenium自动成功发送了电子邮件。

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

答案 4 :(得分:0)

您可以使用此代码使用用于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();
}

答案 5 :(得分:0)

var paramsString = "2=lol&q=how&44=slap&topic=api&1=tr&view=media"; //test url args
var searchParams = new URLSearchParams(paramsString);

// This whitelist of args are the only args to be allowed in the url.
var url_args_whitelist = [
  "beforeafter", "catid", "childforums", "display", "element_id",
  "element_type", "exactname", "filter_mediaType", "filter_order",
  "filter_order_Dir", "filter_search", "filter_tag", "format", "id",
  "Itemid", "layout", "limit", "limitstart", "messageid", "more",
  "option", "order", "ordering", "quality", "query", "recently",
  "recip", "reply_id", "return", "searchdate", "searchf", "searchphrase",
  "searchuser", "searchword", "sortby", "start", "task", "tmpl", "token", "view"
];

// Create an Array from searchParams, then reduce it via ensuring that each 
// key exists within the 'url_args_whitelist' Array, finally joining using 
// an '&' symbol. 
var cleanURL = Array.from(searchParams).reduce(function(array, sub) {
  var key = sub[0], value = sub[1];

  // Check the argument exists in the URL, if so, then push it onto the new array.
  if (url_args_whitelist.indexOf(key) > -1) array.push(key + '=' + value);

  return array;
}, []).join("&");

// Finally a more ES6 style approach, basically a one liner.
const clean = a => l => a.filter(o => l.includes(o[0])).map(o => o.join("=")).join("&");

// Results.
console.log("whitelist output:", cleanURL);
console.log("es6 output:", clean(Array.from(searchParams))(url_args_whitelist));
console.log("old output:", searchParams.toString());