The answers mentioned in How to select specified node within Xpath node sets by index with Selenium? are good enough for my case, but this is not working in selenium RC for me - it doesn't recognize mentioned xPathes with 'element not found' message as well as selenium IDE. Does anybody know how to fix it? Or maybe there are another ways to write correct xpath?
I tried:
(//table)[1]
(.//table)[1]
.(//table)[1]
Also selenium cannot recognize /descendant-or-self::table[1]
thought works well with //descendant-or-self::table[1]
which is another expression unfortunately
Sorry can not provide actual html, but the problem is that different pages has the same data tables which could be wrapped in different elements for example
<div class='PageContentRight'>
<div class='DatePicker'></div>
<div class='PaginationTop'></div>
<table class='AdminSiteDataTable'></table>
<div class='PaginationBottom'></div>
<div></div>
<div></div>
<div></div>
<div class="DataTableWrapper">
<div class='DatePicker'></div>
<div class='PaginationTop'></div>
<table class='AdminSiteDataTable'></table>
<div class='PaginationBottom'></div>
</div>
<div class='SomeDivContainer'>
<div class='SomeClass1'>
<div class='SomeClass2'>
<table class='AdminSiteDataTable'></table>
</div>
<div>
</div>
</div>
That is why I trying to use one PageObject AdminSiteDataTable and send table number to it's constructor, which will set xPathes for current PageObject instance.
public AdminSiteDataTable(int tableNumber){
dataTableXpath = String.format("(//table)[%d]", tableNumber);
dataTableRowByNumberXpath = "//tr[%d]";
dataTableColumnByNumberXpath = "//td[%d]";
dataTableCellByNumberXpath = dataTableXpath + dataTableRowXpath + dataTableColumnXpath;
etc.
}
UPD#1:
I found it works in selenium IDE if I use strict xpath like this:
xpath=(//table)[1]//..//..//..//div[@class='PaginatorLine']
But it not works with WebDriverBackedSelenium, trying to fix it now.
com.thoughtworks.selenium.SeleniumException: The given selector xpath=(//table)[1]//tbody//tr is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression xpath=(//table)[1]//tbody//tr because of the following error:
[Exception... "The expression cannot be converted to return the specified type."
UPD#2: So the only broken method in WebDriverBackedSelenium is getXpathCount() the rest works fine with this selectors like this:
private static final String DATA_TABLE = "xpath=(//table)[%d]";
UPD#3: fixed getXpathCount() like this:
public int xpathCount(String element) {
logger.debug("Counting xPathes: " + element);
if (element.contains("xpath=")) {
return getXpathCount(element.split("xpath=")[1]).intValue();
}
return getXpathCount(element).intValue();
}