0

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();
}
PowerStat
  • 3,757
  • 8
  • 32
  • 57
godblessstrawberry
  • 4,556
  • 2
  • 40
  • 58
  • 1
    Provide the html. Can help you out. – Vinay Dec 23 '13 at 17:17
  • I updated the question - have no ability to provide real html, but tried to clear it out by giving an example – godblessstrawberry Dec 23 '13 at 18:30
  • 2
    Your question is not comprehensible. If you can/may not provide the real HTML, please provide some "playground" HTML to work on. If the problem is reproducible with the HTML (and the div's) you posted, please change your code and question to fit that code. – Jens Erat Dec 23 '13 at 19:31
  • thanks guys, I found the solution: '(//table)[1]' will work for this case - just need to define it as 'xpath=(//table)[1]' – godblessstrawberry Dec 23 '13 at 20:35

0 Answers0