[selenium-users] [rc] How to get Table Names in a page?

157 views
Skip to first unread message

ndk

unread,
May 20, 2010, 2:56:06 PM5/20/10
to Selenium Users
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.

Felipe Knorr Kuhn

unread,
May 20, 2010, 3:15:16 PM5/20/10
to seleniu...@googlegroups.com
Hello,

Have you tried using getXPathCount to iterate over the tables?

int count = selenium.getXpathCount("//table").intValue();
for (int i=1; i <= count; i++){
System.out.println(selenium.getAttribute("//table[" + i + "]@name"));
}

FK

ndk

unread,
May 20, 2010, 3:34:59 PM5/20/10
to Selenium Users
Hello,
Thank your response. I did try your code but no luck :(. Is there
something else I should try?

null
com.thoughtworks.selenium.SeleniumException: Element //table[2] not
found
at
org.openqa.selenium.internal.seleniumemulation.ElementFinder.findElement(ElementFinder.java:
46)
at
org.openqa.selenium.internal.seleniumemulation.GetAttribute.handleSeleneseCommand(GetAttribute.java:
37)
at
org.openqa.selenium.internal.seleniumemulation.GetAttribute.handleSeleneseCommand(GetAttribute.java:
23)
at
org.openqa.selenium.internal.seleniumemulation.SeleneseCommand.apply(SeleneseCommand.java:
30)
at org.openqa.selenium.WebDriverCommandProcessor
$1.call(WebDriverCommandProcessor.java:283)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown
Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

In the code you provided it returns the exact same count (48) which I
had tried from following code using webdriver.
java.util.List<WebElement> lstTables =
driver.findElements(By.tagName("table"));
System.out.println(lstTables.size());

Thanks so much for your reply. This group rocks. I am learning so much
day by day.

Regards,
-Nilesh
> > For more options, visit this group athttp://groups.google.com/group/selenium-users?hl=en.
>
> --
> 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 athttp://groups.google.com/group/selenium-users?hl=en.

Felipe Knorr Kuhn

unread,
May 20, 2010, 3:41:52 PM5/20/10
to seleniu...@googlegroups.com
Hello,

Does your page have frames by any chance?

Also, could you try using an older version of Selenium instead of
WebDriverBackedSelenium?

That piece of code I sent was tested against a random page [1] with
tables, but using @summary instead of @name, so I have no other clues.


FK


[1] http://www.cheltladiescollege.org/college/exams.asp

ndk

unread,
May 20, 2010, 5:05:39 PM5/20/10
to Selenium Users
I think there is only one frame. I looked at the HTML code of the page
and also with the help of code you provided and slight tweaking. Is
this what you asked?
int count = selenium.getXpathCount("//iframe").intValue();
System.out.println(count);
for (int i=1; i <= count; i++){
System.out.println(selenium.getAttribute("//iframe[" + i +
"]@name")); //returns null
System.out.println(selenium.getAttribute("//iframe[" + i +
"]@id")); //returns id matching the one in the html code
}

Thanks,
-Nilesh

On May 20, 3:41 pm, Felipe Knorr Kuhn <fkn...@gmail.com> wrote:
> Hello,
>
> ...
>
> read more »

ColinFine

unread,
May 21, 2010, 10:41:07 AM5/21/10
to Selenium Users


On May 20, 7:56 pm, ndk <nilesh.c...@gmail.com> wrote:
> 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,
>
Why do you think your tables have names? 'Name' is an attribute of
only certain elements in HTML, not including table.

ndk

unread,
May 21, 2010, 11:16:22 AM5/21/10
to Selenium Users

I did not know this. Thanks for the update. I guess my question would
be then how to find table locators instead of names.

Bob McConnell

unread,
May 21, 2010, 11:28:44 AM5/21/10
to seleniu...@googlegroups.com
From: ndk

> I did not know this. Thanks for the update. I guess my question would
> be then how to find table locators instead of names.

When necessary, I have assigned ID attributes to tables where there is
more than one on a page. Otherwise, you will probably have to cobble up
an xpath locator.

Bob McConnell
Reply all
Reply to author
Forward
0 new messages