views:

769

answers:

2

I'm using Selenium RC and created all scripts in java (Using Junit test case)

I want to click one grid column having "Edit" link. My code is look like,

selenium.click("//table[@id='ctl00_POMSContentPlaceHolder_gvBillingCompany']//tr["+gRow+"]//td["+gCol+"]");

gRow and gCol are defined as integers and their values vary as moving to next row/column.

Please let me know if anything needs to be done in this.

+1  A: 

Some notes:

  • You probably want to click on the link (<a>), not the cell.
  • Children selectors need a single slash: //parent/child.
  • I'm pretty sure you need tbody there - it is added implicidly by browsers:
    //table/tbody/tr[2]/td[3]/a[1]
  • You may have better luck with a css selector:
    css=table tr:nth-child(2) td:nth-child(3) a
Kobi
+1 although he does not need to add `tbody` if he writes `//table//tr[2]` as the `//` looks for a descendant not only in the next deeper level, but in all deeper levels. This can make a mess in nested tables though...
moxn
@moxn - good point - so this probably not the problem here.
Kobi
hey Kobi, I include "/tbody" and it works....thank you thank you thank you....woooopppppyyyyyyyy..... Thanks a lot :)
Saara
No problem, Saara, happy to help. Feel free to accept my answer by using the check mark next to it.
Kobi
Adding `tbody` **may** make the locator more fragile. Also, your CSS selector example has a typo - you've used `tr` twice instead of `td` in the second instance.
Dave Hunt
Well spotted, thanks. I agree, it also has potential problems. That's one of the reasons I suggested the css way.
Kobi
Dave - you may be right...but I am new to selenium RC and I do not have any idea to implement css.Kobi - if this "tbody" is not much safe then can you please give me more description about this CSS one.Thanks.
Saara
@Saara - it is a bit more fragile because it depends greatly on the structure of your page, and because it's possible a browser wouldn't add the `tbody` tag (though afaik they do). A CSS locator can be more general (for example, if you have a class on the link or cell, or it has an id). CSS locators are recommended: http://seleniumhq.org/docs/04_selenese_commands.html - Quote: `Most experienced Selenium users recommend CSS as their locating strategy of choice as it’s considerably faster than XPath and can find the most complicated objects in an intrinsic HTML document.`
Kobi
+1  A: 

Try the following:

selenium.click("xpath=id('ctl00_POMSContentPlaceHolder_gvBillingCompany')/descendant::tr[" +gRow+ "]/descendant::td["+gCol+"]");

The following is from http://www.w3.org/TR/xpath#path-abbrev

In XPath, // is shorthand for /descendant-or-self::node()/ but the location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.

Dave Hunt