简体   繁体   English

如何在Selenium Automated Test中管理Gmail中COMPOSE按钮的动态ID

[英]How to manage dynamic id for COMPOSE button in Gmail in Selenium Automated Test

The id of COMPOSE button of Gmail is dynamic. Gmail的COMPOSE按钮的ID是动态的。 So when it is clicked different xpath is recorded by Selenium IDE as follows: 因此,当单击它时,Selenium IDE会记录不同的xpath,如下所示:

//div[@id=':lw']/div/div , //div[@id=':as']/div/div

What can be the alternative ways of using the id or xpath? 使用id或xpath的替代方法是什么?

The following is the HTML for COMPOSE button: 以下是“ COMPOSOSE”按钮的HTML:

<div class="aic" id=":as"><div class="z0"><div tabindex="0" role="button" class="T-I J-J5Ji L3 T-I-KE" style="-moz-user-select: none;" gh="cm">COMPOSE</div></div></div>

在Gmail中将以下代码用于COMPOSE

ClickAt   |  //div[text()='COMPOSE']

You should use CSS classes. 您应该使用CSS类。 They do not change in Gmail and this solution won't be locale dependent. 它们不会在Gmail中更改,并且此解决方案不会依赖于语言环境。

For the compose button the class is "TI J-J5-Ji L3". 对于撰写按钮,类别为“ TI J-J5-Ji L3”。

An example using jQuery: 使用jQuery的示例:

$('.T-I.J-J5-Ji.L3').live('click', function () {

     alert('Compose button clicked');

});

It is better to use Sikuli here. 最好在这里使用Sikuli

Sikuli Script automates anything you see on the screen. Sikuli Script可以自动执行屏幕上显示的所有操作。 It uses image recognition to identify and control GUI components. 它使用图像识别来识别和控制GUI组件。 It is useful when there is no easy access to a GUI's internal or source code. 当无法轻松访问GUI的内部或源代码时,此功能很有用。

The following WebDriver Java code should work nicely: 以下WebDriver Java代码应正常运行:

driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();

Using Selenium RC with Java: 在Java中使用Selenium RC:

selenium.click("//div[text()='COMPOSE']");

google uses dynamic ids for most of the elements. google对大多数元素使用动态ID。 Try using the "Compose" text instead of using the ID. 尝试使用“撰写”文本而不是ID。

you can use something like this. 您可以使用类似这样的东西。 Webelement element = driver.findElementByXpath("//div[text()='COMPOSE']"); Webelement元素= driver.findElementByXpath(“ // div [text()='COMPOSE']”)); element.click(); element.click();

hope this works! 希望这有效!

I have sent an Emil successfully through selenium automation using Gmail Account Below is the script please find it: 我已经使用Gmail帐户通过硒自动化成功发送了一个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM