Click a web element until it is hidden

I have a web application in which I press the submit button until the data on the table is available. When there is no data available in my data, then send the hidden button. That way, we can get the logic until we send the hidden buttons. we show a success message and load the following 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 presses the submit button. And after sending one warning, the browser shows why I accept this. In this cycle, for (k=0;k>30;k++)Blindly, I take 30 .. Is there any logic or any misconception how I can control this ... enter image description here

+4
source share
5 answers

isDisplayed(), ,

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");
}
+1

, : -

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

The code below 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");
        }
 }
0
source

Implement the 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;

    }

He presses until your element becomes invisible after you can continue your action further. Hope this helps.

0
source

In the following way, I solve my problem. Hope this helps the 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));
0
source

Source: https://habr.com/ru/post/1686380/


All Articles