1

enter image description here

I want to add today's count. I am new to selenium, and am not getting how to add using selenium. Can anyone suggest a solution?

This is my code:

package testpk;

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

import java.awt.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("RB0764",Keys.ENTER);
        Thread.sleep(3000);
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys("kanna123",Keys.ENTER);

        Thread.sleep(7000);
        driver.findElement(By.xpath("//*[@data-id='dashboard']")).click();
        Thread.sleep(5000);
        WebElement table = driver.findElement(By.xpath("//*[@id=\"pcoded\"]/div[2]/div/div/div/div/div/div/app-default/div/app-modal-basic/div/div/div/div/div/div/table/tbody/tr[1]/td[1]/p"));

        List listOfRows = (List) table.findElements(By.tagName("tr"));

        System.out.println("Rows: "+listOfRows.size());

        //List<WebElement> listOfCols = listOfRows.get(0).findElements(By.tagName("td")); //If first row is normal row

        List<WebElement> listOfCols = ((WebDriver) listOfRows).get(0).findElements(By.tagName("th")); //If first row is header row

        System.out.println("Columns: "+listOfCols.size());
    }
}
2
  • 2
    I want to help but first you need to learn good locators. Using WebElement table = driver.findElement(By.xpath("//*[@id=\"pcoded\"]/div[2]/div/div/div/div/div/div/app-default/div/app-modal-basic/div/div/div/div/div/div/table/tbody/tr[1]/td[1]/p")); will lead to problems. Find a better shorter identifier and we'll see if that is the problem Commented Mar 21, 2019 at 9:49
  • See blog.mozilla.org/fxtesteng/2013/09/26/… You are using ID's in some cases, which is good, but then having div/div/div/div/div makes them hard to read and brittle - any page layout change, no matter how unrelated, will break your automation which is not what you want. Commented Mar 21, 2019 at 9:52

1 Answer 1

0

We can have the column value counts by using a 'for' loop (if that is what you are looking for).

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

But as Michael mentioned, please use good locators.

4
  • I got ur point sir but in that website id is not there i need complete code to add column count could you please help me to do this Commented Mar 21, 2019 at 11:34
  • I haven't used 'id' anywhere in above code. So that should work as it is. Try once and let us know if you are facing any challenge.
    – Ranjeet
    Commented Mar 21, 2019 at 15:57
  • Thank you sir but stil it showing 2 errors row cant be resolved Commented Mar 25, 2019 at 6:15
  • I have used 'rows' variable instead of 'listOfRows' variable that you have used. Just replace accordingly and try.
    – Ranjeet
    Commented Mar 25, 2019 at 7:29

Your Answer

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

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