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());
}
}
asked Mar 21, 2019 at 5:21
1 Answer 1
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.
-
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 thischaitra teppad– chaitra teppad2019年03月21日 11:34:32 +00:00Commented 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– Ranjeet2019年03月21日 15:57:56 +00:00Commented Mar 21, 2019 at 15:57
-
Thank you sir but stil it showing 2 errors row cant be resolvedchaitra teppad– chaitra teppad2019年03月25日 06:15:06 +00:00Commented 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– Ranjeet2019年03月25日 07:29:22 +00:00Commented Mar 25, 2019 at 7:29
Explore related questions
See similar questions with these tags.
default
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 problemdiv/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.