How to code an if statement if it is a WebElement?

I am trying to manipulate these blocks of codes:

List<WebElement> more = driver.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button"));
        if(more.size()!=0){
                driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button")).click();
        }else {
            WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
            List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
            System.out.println("Total Number of TR: " + list.size());
        }

      

What I'm trying to do hopefully is that before I have done what is in the IF statement, I want every loop to see the element / html / body / div [1] / div / div [ 3] / div / div / div [1] / div / div [2] / div [2] / div / div [4] / button will be clicked and if it is not available, do the following:

WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
            List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
            System.out.println("Total Number of TR: " + list.size());

      

Hello guys, for more information on what I'm trying to do here is this. I am listed for a specific module and then the button has "Click here for more entries" -> its XPath - /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button

I want that if I am in the list and this button is present as above, I want to click it. And if there is no "Click here for more entries" button in the list (for example, the entries are 5 entries so there is no clear pagination) I want to execute blocks of code

+3


source to share


1 answer


What I understood from your query is that you want to loop through the condition's IF statement multiple times until the else condition is satisfied. To do this, you can try the following code:

     (1) try{
      (2)    String xpathVAL="/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button";
         (3) int i=1;
       (4)   while(i!=0){

              (5)   if(driver.findElements(By.xpath(xpathVAL)).size() != 0)

             (6)    {
                (7)      driver.findElement(By.xpath(xpathVAL).click();
                (8)   Thread.sleep(2000);

               (9)  }

             (10)    else{
              (11)              Thread.sleep(2000);
              (12)         WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
    (13)    List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
     (14)   LOGGER.debug("Total Number of TR: " + list.size());
        (15) i=0;
        (16)         }
      (17)    }
    (18)      }catch(Exception e){
        (19)  LOGGER.debug("Exception Caught");
(20) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
       List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));

          }

      

I think your requirement would be to click the button until some window appears and the button disappears. What I suggest for this is to apply "Thread.sleep (// some 3-4 seconds //)". However, this is bad coding style and the use of a power thread is not suggested by many standards. You can add another implicit pending . Also, if your item is not found after a certain period of time, you can apply an abort condition after a certain time interval, otherwise it will go into an infinite loop. If you have any problems writing code, let me know and I will help you with it.



EDIT

Try following points to remove item missing from cache: -

  • Use Explicit wait (Thread.sleep) on line 8 and line 11
  • Change the xpath from position to some type id / name / class. Changing it to NAME allows you to get a clearer image of the element, even if its position has changed.
  • Reinitialize the element again if the exception hits on line 20.
0


source







All Articles