有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

打开并阅读带有Selenium/Katalon(Java)特定标题的电子邮件文本(来自Gmail)

我想:

  • 打开Gmail
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();
  • 打开并存储具有该标题的所有电子邮件的文本(可能位于不同的页面)

我怎么做


共 (1) 个答案

  1. # 1 楼答案

    好的,在你搜索了文本之后,我想下一件事应该是找出如何单击当前页面上的每个元素,然后处理分页

    对我来说,以下CSS选择器可以找到页面上的所有电子邮件元素:

    WebElement[] listOfEmailElements = driver.findElements(By.cssSelector('tbody > tr.zA'));
    

    例如,如果该页面上有50个结果,它将返回一个包含这50个WebElement的数组

    现在,我们必须循环浏览电子邮件,点击每一封,然后点击。。。图标并下载电子邮件,然后返回到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();
        }
    }