我想要:
driver = new ChromeDriver();
driver.get("https://mail.google.com/mail/#inbox");
driver.findElement(By.xpath("//input[@name='q']")).click();
driver.findElement(By.xpath("//input[@name='q']")).clear();
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("Title");
driver.findElement(By.cssSelector("button.gb_2e.gb_df > svg")).click();
我该怎么做?
好的,所以在你搜索了你的文本之后,我猜接下来的事情就是弄清楚如何点击当前页面上的每个元素,然后处理分页。
对我来说,以下CSS选择器找到页面上的所有电子邮件元素:
WebElement[] listOfEmailElements = driver.findElements(By.cssSelector('tbody > tr.zA'));
例如,如果您在该页面上有50个结果,则它将返回包含这50个webElements的数组。
现在,我们必须遍历电子邮件,点击每个电子邮件,然后单击...图标并下载电子邮件,并返回到for循环中的所有主页面。
for (WebElement element: listOfEmailElements) {
element[i].click();
//The following is the only way I could find to uniquely identify the '...' icon
driver.findElement(By.cssSelector('div.T-I.J-J5-Ji.T-I-Js-Gs.aap.T-I-awG.T-I-ax7.L3').click();
//Couldn't find a way to uniquely identify the "Download" button but the following selector gets
//an array of options from the list you just opened above and download is at an index of 16
driver.findElements(By.cssSelector('div.cj'))[16].click();
//simply use the browser's back button to navigate back to the main list
driver.navigate().back();
}
好的,现在我们只需处理分页。
它看起来像插入到下一页的插入符号图标,当启用时,由唯一标识
driver.findElement(By.cssSelector('div.T-I.J-J5-Ji.amD.T-I-awG.T-I-ax7.T-I-Js-Gs.L3'))
但是,当禁用该按钮时,该选择器会找到两个元素。我认为这使得以下代码可以满足您的需求:
while (driver.findElements(By.cssSelector('div.T-I.J-J5-Ji.amD.T-I-awG.T-I-ax7.T-I-Js-Gs.L3')).size() == 1) {
for (WebElement element: listOfEmailElements) {
element[i].click();
//The following is the only way I could find to uniquely identify the '...' icon
driver.findElement(By.cssSelector('div.T-I.J-J5-Ji.T-I-Js-Gs.aap.T-I-awG.T-I-ax7.L3').click();
//Couldn't find a way to uniquely identify the "Download" button but the following selector gets
//an array of options from the list you just opened above and download is at an index of 16
driver.findElements(By.cssSelector('div.cj'))[16].click();
//simply use the browser's back button to navigate back to the main list
driver.navigate().back();
}
}