Click webelement until hidden

Viewed 1713

I have a web application where I press submit button until data available on the table.When in my no data are available then submit button hidden.So we can get logic until submitting button hides we will click.And when button, not available we show on success message and load next browser Url.

for (k=0;k>30;k++) {
    try {
       driver.findElement(By.xpath(".//*[@id='content']/input")).click();
       driver.switchTo().alert();
       driver.switchTo().alert().accept();
       Thread.sleep(20000);
    } catch (org.openqa.selenium.NoSuchElementException e){
       System.out.println(""+location+"  Done");
    }
}

Here driver.findElement(By.xpath(".//*[@id='content']/input")).click(); this line click my submit button.And after submit one browser alert shows thats why i accept this. In this loop for (k=0;k>30;k++) Blindly i take 30..Is there any logic or any sugggestion how can i manage this... enter image description here

5 Answers

You can use existence of the element in a way something like this:-

By by = By.xpath(".//*[@id='content']/input");
List<WebElement> buttons = driver.findElement(by);

while(buttons !=null && !buttons.isEmpty()) {
    WebElement yourButton = buttons.get(0); // assuming only and the first element in the list
    yourButton.click();
    driver.switchTo().alert();
    driver.switchTo().alert().accept(); // ideally this should be sufficient
    Thread.sleep(20000);
    buttons = driver.findElement(by);
}

Below code may help you.

 try {
      while(driver.findElement(By.xpath(".//*[@id='content']/input")).isDisplayed()){
       driver.findElement(By.xpath(".//*[@id='content']/input")).click();
       driver.switchTo().alert();
       driver.switchTo().alert().accept();
       Thread.sleep(20000);
     }
    }
    catch (org.openqa.selenium.NoSuchElementException e){
       System.out.println(""+location+"  Done");
    }

another solution with findElements,

      while(driver.findElements(By.xpath(".//*[@id='content']/input")).size()>0)
      {
        try {
            driver.findElement(By.xpath(".//*[@id='content']/input")).click();
            driver.switchTo().alert();
            driver.switchTo().alert().accept();
            Thread.sleep(20000);
        }
        catch (org.openqa.selenium.NoSuchElementException e){
            System.out.println(""+location+"  Done");
        }
 }

Simply checked with isDisplayed() to check whether element is displayed or not

WebElement ele=driver.findElement(By.xpath(".//*[@id='content']/input"));
//check element is present or not
try {
if(ele.size()>0){
   if(ele.isDisplayed()){
   ele.click();
   }
   }
   //switch to alert and perform operation 
   driver.switchTo().alert();
   driver.switchTo().alert().accept();
   Thread.sleep(20000);
} 
 catch (Exception e){
   System.out.println(""+location+"  Done");
}

Implement below Logic :

public void test()  throws InterruptedException 

{
    WebElement button = driver.findElement(By.xpath(".//*[@id='content']/input"));
    while(checkButton(button))
    {
        button.click();
        driver.switchTo().alert().accept();
        Thread.sleep(1000);
    }


}               
    public static boolean checkButton(WebElement element)
    {
        if(element.isDisplayed())
        return true;
        else
            return false;

    }

It will click until your element get invisible once it your can perform your action further. Hope this will help.

Following way I solve my Problem..Hope Its will help to next visitor

 do
   {
   try {

   driver.findElement(By.xpath(".//*[@name='yt1']")).click();
   driver.switchTo().alert();
   driver.switchTo().alert().accept();
   Thread.sleep(20000);
   driver.switchTo().alert();
   driver.switchTo().alert().accept();
   Thread.sleep(2000);
   driver.navigate().refresh();

   }
   catch (org.openqa.selenium.NoSuchElementException e){
   System.out.println(""+location+"  Done");
    }
    }
     while ((driver.findElements(By.xpath(".//*[@class='google_data_save']")).size()>0));
Related