Div 作为按钮的标签,以及删除、报告垃圾邮件等动态按钮

Div tag acting as buttons, and also Dynamic buttons like delete, report spam, etc

这是一个练习测试用例,我必须登录到 gmail 并单击动态网络中的所有复选框 table 并删除邮件。所以我做了以下代码。

问题是当我检查删除按钮是否可用时。它正在返回 true 但当我尝试执行删除操作时它显示 ElementNotVisibleException。仅供参考,我可以 select 所有复选框。唯一的问题是点击标签制作的按钮。

//deleting mail by clicking on all checkbox     
int count = 1;     
List<WebElement> lst = driver.findElements(By.xpath(cbox));   
System.out.println("Total number of checkboxes are \t: " +    lst.size());    
for(int i=0;i<lst.size();i++){         
  WebElement wwe = lst.get(i);  
  wwe.click(); 
  driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 
  System.out.println("Checked on checkbox number \t: " + count); 
  count++; 
} 
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 
try{ 
  boolean flag = driver.findElement(By.xpath(delete)).isEnabled(); 
  if(flag){ 
    System.out.println("\nDelete button is enabled"); 
  }else{ 
    System.out.println("\nDelete button is not enabled"); 
  } 
  driver.findElement(By.xpath(delete)).click(); 
}catch(Throwable t){ 
  System.out.println("\nUnable to locate delete button"); 
  System.out.println("The exception occuring is \t: " + t); 
}

您可能选择了像 Gmail 这样不太自动化的 Web 应用程序作为开始。我相信他们故意开发了 Gmail 客户端,使机器人更难执行操作。

关于你的问题,我认为删除按钮是在选中复选框后出现的。所以我相信你必须明确地等待按钮出现。也有可能是你的xpath不对。

你可以试试这个,

WebDriverWait wait = new WebDriverWait(driver, 60 /*timeOut in Seconds*/);
wait.until(ExpectedConditions.
visibilityOfElementLocated(By.css("div[data-tooltip='Delete']"))).click();

我已经尝试了以下方法并且有效fine.You 只需添加足够的等待时间

    WebDriver driver = new FirefoxDriver();
    WebDriverWait wait = new WebDriverWait(driver, 60 /*timeOut in Seconds*/);
    driver.get("https://www.gmail.com");
    driver.findElement(By.id("Email")).sendKeys("xxx");
    driver.findElement(By.id("next")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd"))).sendKeys("xxx");
    driver.findElement(By.id("signIn")).click();
    String cbox = "//table[@class='F cf zt']//div[@class='T-Jo-auh']";
    String delete = "//div[@class='asa']/div[@class='ar9 T-I-J3 J-J5-Ji']";

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cbox)));

    int count = 1;
    List<WebElement> lst = driver.findElements(By.xpath(cbox));
    System.out.println("Total number of checkboxes are \t: " + lst.size());
    for (int i = 0; i < lst.size(); i++) {
        WebElement wwe = lst.get(i);
        wwe.click();
        System.out.println("Checked on checkbox number \t: " + count);
        count++;
    }

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(delete))).click();
    try {
        WebElement deleteButton = driver.findElement(By.xpath(delete));
        boolean flag = deleteButton.isEnabled();
        if (flag) {
            System.out.println("\nDelete button is enabled");
        } else {
            System.out.println("\nDelete button is not enabled");
        }
        deleteButton.click();
    } catch (Throwable t) {
        System.out.println("\nUnable to locate delete button");
        System.out.println("The exception occuring is \t: " + t);
    }