小编典典

Div标签既充当按钮,也充当动态按钮,例如删除,报告垃圾邮件等

selenium

这是一个实践测试用例,其中我必须登录gmail并单击动态Web表中的所有复选框,然后删除邮件。所以我做了下面的代码。

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

//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); 
}

阅读 302

收藏
2020-06-26

共1个答案

小编典典

我已经尝试了以下方法,但效果很好。您只需添加足够的等待时间

    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);
    }
2020-06-26