Eclipse IDE で Java を使用して Selenium Webdriver を使用して、Gmail の受信トレイからメールを開く必要があります。xpathを使用してこれを行う方法はありますか?
44096 次
6 に答える
8
理想的な方法は、Selenium を使用して gmail を自動化するのではなく、Gmail API ( https://developers.google.com/gmail/api/#how_do_i_find_out_more ) を使用してメッセージが正常に送信されたことを確認することです。API レベルでメッセージをチェックする方法を学びたくない場合は、このリンクを gmail の初期 URL として使用して、HTML バージョンの gmail を使用することを強くお勧めします ( https://mail.google.com/mail/?ui =html ) JavaScript を有効にして gmail を使用すると、信頼できるテスト スクリプトを作成することが非常に難しくなります。
于 2016-04-21T06:22:08.220 に答える
0
こんにちは、以下のコードを試してみてください。未読メールのみをチェックします
public static void main(String[] args) {
// TODO Auto-generated method stub.
System.setProperty("webdriver.chrome.driver","D:\\eclipseProject\\StackOverFlow\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://accounts.google.com/ServiceLogin?");
// gmail login
driver.findElement(By.id("Email")).sendKeys("your gmail username");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("your gmail password");
driver.findElement(By.id("signIn")).click();
// some optional actions for reaching gmail inbox
driver.findElement(By.xpath("//*[@title='Google apps']")).click();
driver.findElement(By.id("gb23")).click();
// now talking un-read email form inbox into a list
List<WebElement> unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
// Mailer name for which i want to check do i have an email in my inbox
String MyMailer = "Stack over flow";
// real logic starts here
for(int i=0;i<unreademeil.size();i++){
if(unreademeil.get(i).isDisplayed()==true){
// now verify if you have got mail form a specific mailer (Note Un-read mails)
// for read mails xpath loactor will change but logic will remain same
if(unreademeil.get(i).getText().equals(MyMailer)){
System.out.println("Yes we have got mail form " + MyMailer);
// also you can perform more actions here
// like if you want to open email form the mailer
break;
}else{
System.out.println("No mail form " + MyMailer);
}
}
}
}
于 2016-04-21T05:27:19.917 に答える