Home > database >  How to get particular row values in dynamic table if the 'text ' matches in Selenium Java
How to get particular row values in dynamic table if the 'text ' matches in Selenium Java

Time:12-19

I can able to print all the values in webtable but not sure how to get particular row value. Here i need print respective row value where text matches 'Aiden' because Aiden text might display in any of the row number there is no fixed position. It will be very helpful if someone resolve the issue.

driver.get("https://demoqa.com/elements");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//clicking the left menu

          driver.findElement(By.xpath("//span[text()='Web Tables']")).click(); 
                  
          //driver.findElement(By.xpath("//div[@class='rt-table']")).click();
          Thread.sleep(5000);

// selecting the table

          WebElement table = driver.findElement(By.xpath("//div[@class='rt-table']"));  

//selecting the table header

            List<WebElement> tableheaders = table.findElements(By.xpath("//div[@class='rt-thead -header']"));  

//printing all the table header values

            for (WebElement headername : tableheaders) {   
                String text = headername.getText();
                System.out.println(text);
            }

//selecting all row values

            List<WebElement> allrows = table.findElements(By.xpath("//div[@role='row' and @class='rt-tr -odd' or @class='rt-tr -even']")); 

// total row count

            int size = allrows.size();
            System.out.println("Rows size: " size); 

// selecting all column values

            for (int colnum = 0; colnum < size; colnum  ) {
                List<WebElement> colums = table.findElements(By.xpath("//div[@class='rt-td' and @role='gridcell'] /..")); 
                WebElement firstColumn = colums.get(colnum);
                System.out.println(firstColumn.getText());
            }

CodePudding user response:

You can create an empty string variable and assign its value when the row contains the given keyword

        String rowValue = "";
        for (WebElement webElement : tableRows) {
            if (webElement.getText().contains(searchedValue)) {
                rowValue = webElement.getText();
            }
        }
        System.out.println(rowValue);


  
  • Related