Hi all,
Is there any way I can scan through a page and get all table names and
then work with the table of interest? Essentially what I am trying to
do is get the contents in the table. I thought of two approaches, one
I am having trouble with and other is probably not that efficient.
Here is what I had been trying,
1. To get the data in the table, I thought I will use the
selenium.getTable("tablename.row.col"). The problem here is I am not
able to find the table name and would appreciate any pointers here. I
used IE Developers toolbar and saw the properties of the table. The id
of the table is "ext-gen474" and class is "x-grid3-body". I couldn't
find the name of the table though. Obviously selenium.getTable("ext-
gen474.row.col") does not work since ext-gen474 is an id not name of
the table.
2. In my second approach I used the Webdriver (see code below). I scan
for the class name I am looking for, ignore the empty tables and
getText of the table which is not empty (Currently there is only one
table in my webpage that is not empty, two seem to be empty). So I get
all data in the table. I still think this is not the ideal way.
Essentially we should be able to access the table by referencing row
and column like approach one. What if I have multiple tables in the
webpage? It would be hard to validate the table of interest, right?
I would appreciate any help regarding this,
Thanks,
-Nilesh
This is the code I used,
java.util.List<WebElement> lstTables =
driver.findElements(By.className("x-grid3-body"));
for(WebElement table:lstTables) {
String s = table.getText();
System.out.println(s.isEmpty());
if(!s.isEmpty()) {
System.out.println("My id "+ table.getAttribute("id")); //Not
consistent across different browsers
System.out.println("My Tagname\n"+ table.getTagName()); //tag name
comes out as "table"
System.out.println("My table:"+ i + "\n"+ table.getText()); //Get
all table contents here- works just fine
System.out.println("Here is the value\n"+
selenium.getTable(table.getAttribute("id")+".2.2") ); //This one
obviosuly does not work
}
}
//Also a few different approaches//
//Approach 1-Does not throw any exception but does not find any
table//
java.util.List<WebElement> lstallTables =
driver.findElements(By.id("table"));
System.out.println("Size of tables is "+ lstallTables.size()); //
Total number of pages come out as 0, does not help :(
for(WebElement table:lstallTables) {
System.out.print(table.getText());
java.util.List<WebElement> lstallRows =
table.findElements(By.id("tr"));
for(WebElement row:lstallRows) {
java.util.List<WebElement> cells = row.findElements(By.id("td"));
for(WebElement cell:cells) {
System.out.print(cell.getText());
}
System.out.println("\n");
}
}
//Approach 2//
WebElement table = driver.findElement(By.id("table"));
java.util.List<WebElement> lstallRows =
table.findElements(By.id("tr"));
for(WebElement row:lstallRows) {
java.util.List<WebElement> cells = row.findElements(By.id("td"));
for(WebElement cell:cells) {
System.out.print(cell.getText());
}
System.out.println("\n");
}
//Exception//
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"id","selector":"table"}
System info:
os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1',
java.version: '1.6.0_19'
Driver info: driver.version: firefox
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at
org.openqa.selenium.firefox.Response.ifNecessaryThrow(Response.java:
105)
at
org.openqa.selenium.firefox.FirefoxDriver.executeCommand(FirefoxDriver.java:
331)
at
org.openqa.selenium.firefox.FirefoxDriver.sendMessage(FirefoxDriver.java:
312)
at
org.openqa.selenium.firefox.FirefoxDriver.sendMessage(FirefoxDriver.java:
308)
at
org.openqa.selenium.firefox.FirefoxDriver.findElement(FirefoxDriver.java:
265)
at
org.openqa.selenium.firefox.FirefoxDriver.findElementById(FirefoxDriver.java:
179)
at
org.openqa.selenium.By$1.findElement(By.java:66)
at
org.openqa.selenium.firefox.FirefoxDriver.findElement(FirefoxDriver.java:
175)
at
com.testscripts.WebDriverTesting.getTableElements(WebDriverTesting.java:
150)
at com.testscripts.WebDriverTesting.main(WebDriverTesting.java:196)
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to
seleniu...@googlegroups.com.
To unsubscribe from this group, send email to
selenium-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/selenium-users?hl=en.