J Mullen J Mullen - 4 months ago 7
Java Question

Finding a specific table with no id

I am working on my first selenium project and I have hit a bit of a road block. I have a webpage that contains 3 different tables. I only want to pull data from one of these tables. The problem I am facing is that bootstrap was used to style these pages, so all of the tables share the same class, and none of them have an id that I can specifically search for. I was able to do this on a different page by search for an xpath for tr/td, but that isn't working for this page because of the multiple tables. Inspecting the page source for the table I want to pull the data from I get something like the following:

<div class="col-md-4">
<div class="panel panel-success">
<div class="panel-panel-heading>
<h3 class="panel-title">table title</h3>
</div>
<div class="table-responsive">
<table class="table-striped table-bordered">
<thead>...</thead>
<tbody>
<tr ng-repeat="element in main.elementService.elementTable" class="ng-scope">
<td class="ng-binding">info1</td>
<td class="ng-binding">info2</td>
<td class="ng-binding">info3</td>
<td class="ng-binding">info4</td>
</tr>
<tr ng-repeat="element in main.elementService.elementTable" class="ng-scope">
<tr ng-repeat="element in main.elementService.elementTable" class="ng-scope">
<tr ng-repeat="element in main.elementService.elementTable" class="ng-scope">
</tbody>
</table>
</div>




So this leads me to the problem that I am not sure what I can use to select only this table. If I search for
tr
or
td
I will get data from the other tables on the page aswell.

The only unique feature to this table that I see is the
<div class="col-md-4">.

I don't know if it is possible, but my idea was to do some sort of a combination search to go to this specific div and then go to the xpath tr/td to pull the data from each cell of the table, but I am new to selenium and I am not sure how to go about doing this. The documentation I am looking at does not seem to show how to combine multiple strategies together, and I am having trouble wording this for a google search, so I haven't found a google link that has given me any insight into how to do this.

I've tried something like this, but I don't think my syntax is correct

List<WebElement> TableElems = chrome.findElements(By.xpath("//div[@class='col-md-4']//tr/td"));


I also tried

List<WebElement> TableElems = chrome.findElements(By.xpath("//div[@class='col-md-4']" && "//tr/td"));


but the && operator is not allowed here.

If anyone has any ideas to tip me into the right direction I would appreciate it.

Answer

You can use this xpath expression:

"//div[@class='col-md-4']//tr/td"

The difference to your attempt is the use of the class attribute (there is no id) and the double slash before tr which allows the tr to be a descendant element of div[@id='col-md-4'] in any depth and not just a direct child.

Comments