java 使用 Selenium WebDriver 在 Gmail 中单击“撰写”按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26390634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clicking Compose button in Gmail using Selenium WebDriver
提问by Luke Smith
The button pressing command doesn't work. It's finding the button, but isn't clicking the button. When clicking the button there should be a native page that opens within Gmail.
按钮按下命令不起作用。它正在查找按钮,但没有单击按钮。单击该按钮时,应该会在 Gmail 中打开一个本机页面。
All the code below is attempting to click the button within the new contacts page of Gmail https://mail.google.com/mail/u/0/1#contact/new
下面的所有代码都试图点击 Gmail https://mail.google.com/mail/u/0/1#contact/new的新联系人页面中的按钮
Inspecting the element the div tag is 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>
检查元素 div 标签是 div tabindex="0" aria-label="Email" data-tooltip="Email" aria-disabled="false" style="-moz-user-select: none;" id=":2l" class="TI J-J5-Ji TI-ax7 TI-Js-IF L3" role="button">div class="J-J5-Ji TI-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();
Please let me know if you can help me identify this button
如果你能帮我识别这个按钮,请告诉我
回答by Avinash Pande
This method uses 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("[email protected]");
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("[email protected]");
driver.findElement(By.xpath("//input[@name='subjectbox']")).sendKeys("[email protected]");
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();
}
}
回答by kai
I find the send button like this:
我发现发送按钮是这样的:
driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();
After that you can do a quit. Just have an extra pop up ask you to confirm leaving the account:
之后你就可以戒烟了。只是有一个额外的弹出窗口要求您确认离开帐户:
driver.Navigate().GoToUrl("https://mail.google.com/mail/logout?hl=en");
回答by AndyPerfect
When I look at that page in the link you have provided, the compose button is grayed out and is not clickable. Having a program trying to click a button that a user could not click is still going to fail. Selenium will not and can not interact with objects that a user could not interact with (such as hidden fields, and in this case, grayed out buttons).
当我在您提供的链接中查看该页面时,撰写按钮呈灰色且不可点击。让程序尝试点击用户无法点击的按钮仍然会失败。Selenium 不会也不能与用户无法交互的对象(例如隐藏字段,在本例中为灰色按钮)进行交互。
回答by Avinash Pande
I have sent an Emil successfully through selenium automation using Gmail Account with the below script.
我使用 Gmail 帐户和以下脚本通过 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']", "[email protected]"); // 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
回答by Jnaneshwar Chavan
You can use this code to compose email using selenium web driver for gmail
您可以使用此代码使用 selenium web driver for 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();
}
回答by Akongnwi Devert
driver.findElement(By.xpath("//*[@role='button' and text()='Compose']")).click();