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

前端 未结 2 1907
失恋的感觉
失恋的感觉 2021-01-22 20:16

This is a practice test case where i have to login to gmail and click on all the checkbox in the dynamic web table and delete the mails. So i made the following code.

Th

相关标签:
2条回答
  • 2021-01-22 20:49

    You probably chose not so automation friendly web app like Gmail to start with. I believe they have deliberately developed Gmail client side in such a way that its harder for a Robot to perform actions.

    As for your question, I think the delete button appears a little after check boxes are clicked. So I believe you will have to explicitly wait for the button to appear. It's also possible that your xpath is not correct.

    You could try this,

    WebDriverWait wait = new WebDriverWait(driver, 60 /*timeOut in Seconds*/);
    wait.until(ExpectedConditions.
    visibilityOfElementLocated(By.css("div[data-tooltip='Delete']"))).click();
    
    0 讨论(0)
  • 2021-01-22 21:00

    I've tried the following and it worked fine.You just have to add enough wait

        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);
        }
    
    0 讨论(0)
提交回复
热议问题