2017-02-12 116 views
1

我正在做硒实践,并希望从我的Gmail收件箱中删除约30K条未读邮件。我困于选择未读复选框。尝试了很多Locators和xpath,但是我的xpath选择了所有邮件的复选框。SeleniumJava:删除Gmail中的未读邮件

任何人都可以建议如何选择封未读邮件的复选框

PFB的Java代码硒

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Unread_Gmail { 
public static void main(String[] args) throws InterruptedException { 
    WebDriver driver; 

driver = new FirefoxDriver(); 
driver.manage().deleteAllCookies(); 
driver.manage().window().maximize(); 
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

driver.get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1#identifier"); 

driver.findElement(By.id("Email")).sendKeys("*********@gmail.com"); 
driver.findElement(By.id("next")).click(); 
driver.findElement(By.id("Passwd")).sendKeys("******"); 
driver.findElement(By.id("signIn")).click(); 


Thread.sleep(3000); 


driver.findElement(By.xpath("//div[@class='G-tF']/div[1]/div")).click(); 
driver.findElement(By.xpath(".//*[@id=':z2']/div")).click(); 

//driver.findElement(By.xpath("//div[@class='J-J5-Ji J-JN-M-I-Jm'][1]")).click(); 

//driver.findElement(By.xpath("//div[@class='J-J5-Ji J-JN-M-I-Jm']")).click(); 
//driver.findElement(By.id("z2")).click(); 
//driver.findElement(By.xpath("//div[@class='J-N-Jz']")).click(); 
//driver.findElement(By.id("z2")).click(); 

} 
} 

回答

3

你熟悉Gmail API?如果你只想练习定位器等等,找到复选框是一条路,但在真实生活中,当你必须验证/测试smtp服务器时,使用API​​是最好的选择。每个电子邮件(线程)都有一个label,调用messagesUnread将返回无邮件的邮件。您还可以使用查询,这无论是在API和UI:

in:inbox is:unread 
1

在我的情况下,“未读”下拉菜单选项定位器看起来是这样的:

<div class="J-N" selector="unread" role="menuitem" id=":dz" style="user-select: none;"> 
    <div class="J-N-Jz" style="user-select: none;">Unread</div> 
</div> 

看起来你需要看看对于类“JN”和/或id“:dz”的元素。

+0

我检查过HTML DOM和ID是动态的.... –

+0

试着寻找@selector =“未读”然后,它应该稳定 – Gyrotank

0

您可以尝试给出元素的编号,如12,然后尝试单击。

driver.findElements(By.xpath("//*[@role='checkbox']")).get(1).click(); 
相关问题