0

I have written code like this

package testpk;

import org.openqa.selenium.By;        
import org.openqa.selenium.WebDriver;        
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

import org.openqa.selenium.*;

public class Form {                


    public static void main(String[] args) throws InterruptedException {                                    

        // declaration and instantiation of objects/variables        

        WebDriver driver = new FirefoxDriver();

        driver.manage().window().maximize();
        driver.get("http://beta-app.1bridge.in/#/auth/login"); 
        Thread.sleep(6000);

        driver.findElement(By.xpath("//input[@type='text']")).sendKeys("bbbb",Keys.ENTER);
        Thread.sleep(3000);
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys("123",Keys.ENTER);

        Thread.sleep(7000);
        driver.findElement(By.xpath("//*[@data-id='dashboard']")).click();
        Thread.sleep(5000);
        WebElement table = driver.findElement(By.xpath("#pcoded > div.pcoded-container.navbar-wrapper > div > div > div > div > div > div > app-default > div > app-modal-basic > div > div > div > div > div > div > table > tbody > tr:nth-child(1) > td:nth-child(2)"));

        int intColValue=0;
        int sum=0;
        String strColValue;

        //Loop through all the rows
        for(int i=0;i<rows.size();i++)
        {
            //Get the columns in particular row
            List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));

            //Here I am assuming values are 'String' type
            //Also, I have hard-corded the column number (which you should not do and use column name as input) 
            strColValue = cols.get(2).getText();
            //And convert those to 'Integer'
            intColValue = Integer.parseInt(strColValue);

            //Get the sum
            sum = sum+intColValue;
        }

        System.out.println("Sum: "+sum);
    }

}

It showing error like this Exception in thread "main" java.lang.Error: Unresolved compilation problems:

rows cannot be resolved rows cannot be resolved

at testpk.Form.main(Form.java:38)

enter image description here

I want to add those row elements can anyone please guide me

3
  • Can anyone please suggust me Mar 25, 2019 at 6:10
  • Apart from the issue you're facing, here: driver.findElement(By.xpath("#pcoded > div.pcoded-container.navbar-wrapper > div > div > div > div > div > div > app-default > div > app-modal-basic > div > div > div > div > div > div > table > tbody > tr:nth-child(1) > td:nth-child(2)")); you are trying to look up element using xPath locator but provide css locator.
    – Alexey R.
    Mar 25, 2019 at 9:16
  • java is telling you that it doesn't know what rows is because the code doesn't ever create or set a variable named rows that it can see.
    – mrfreester
    Mar 25, 2019 at 17:16

2 Answers 2

1

Add below line in your code.

This will work for you if you want to sum today's value:-

      driver.findElement(By.xpath("//*[@data-id='dashboard']")).click();
      //this will count all <TR> in your current page.
      List<WebElement> element= driver.findElements(By.tagName("tr"));
      //System.out.println(element.size());
      int integerValue;
      int sumValue=0;
      for(int i=1;i<=element.size()-1;i++)
      {
          //Here enter your Xpath value for the table.
          WebElement todaysValue= driver.findElement(By.xpath("//table[@id='example']//tr["+i+"]/td[2]"));
          String tableValue=todaysValue.getText();
          integerValue=Integer.parseInt(tableValue);
          sumValue=sumValue+integerValue;         
      }
      System.out.println("Total Sum : "+sumValue);  

Also avoid using Thread.sleep in your script this will slow down your script and it's a bad practice, try to use Explicit wait in your script.

0

Sure.

Add below line before 'for' loop.

List<WebElement> rows = table.findElements(By.tagName("tr"));

This would allow us to get all rows in the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.