I'm trying to pull some data out of a table using Selenium. The page I am trying to pull the data from has 2 separate tables on the page, and the data I need is in the second table. I have pointed to the second table using it's ID, but I keep getting back the data from the first table. Here is the piece of code I am using to pull from the table.
WebElement table = driver.findElement(By.id("tblSBResults"));
String date = table.findElement(By.xpath("//tbody/tr[1]/td[1]")).getText();
I have also tried the xpath as //table/tbody/tr[1]/td[1].
And here is what the table I'm trying to pull from looks like.
<table>
<tbody>
<tr>
<td>some data</td>
<td>more data</td?
</tr>
</tbody>
</table>
...
...
<table id = "tblSBResults">
<tbody>
<tr>
<td>6/9/2015</td>
<td>More data that I need</td>
<td>more needed data</td>
</tr>
<tr>
<td>more etc</td>
<td>I think you get the point</td>
</tr>
</tbody>
</table>
Any ideas would be greatly appreciated. I'm using the Java version of Selenium by the way.
Thanks!
Using // will start searching at top of page.
Try this:
WebElement table = driver.findElement(By.id("tblSBResults"));
String date = table.findElement(By.xpath("tbody/tr[1]/td[1]")).getText();
Related
I am working on an app where I am parsing some data from one or two websites. Luckily I did it for some of my targeted data but not. Now that I am using Jsoup for parsing the data from a website I used same jsoup format to get data of phase 2 as I did for phase 1 of my app but this time nothing is fetching arraylist showing up blank. I checked both HTML codes and there is a bit of difference in both.
In my phase1 i parsed the table using it's class and then i get the respective of that table. In the 2nd phase, the format of table and its tr & tds are different so i am struggling to figure it out. I am posting the html code from which i want to get data.
<div class="view-content">
<table class="views-table cols-3">
<thead>
</thead>
<tbody>
<tr class="odd views-row-first views-row-last">
<td class="views-field views-field-counter">
1 </td>
<td class="views-field views-field-body">
<p>some text here</p>
</td>
<td class="views-field views-field-field-notif-pdf">
Size :- 1.85 MB, Language:- English</td>
</tr>
</tbody>
</table>
</div>
I want the data inside above table tag and i am having problems to figure it out how it will be done with all classes in tr and td. Any help or suggestion will be highly appreciated..
THANK YOU!
You can use selectors in Jsoup:
File input = new File("path_to_html/test.html");
Document doc = Jsoup.parse(input, StandardCharsets.UTF_8.name());
///select table body
Element tbody = doc.select("tbody").first();
other examples at:
https://jsoup.org/cookbook/extracting-data/selector-syntax
I have the following table:
<table>
<tbody>
<tr>
<td>
</td>
<td>
User1
</td>
</tr>
<tr>
<td>
</td>
<td>
User2
</td>
</tr>
</tbody>
</table>
I want to find the a-tag of the tr, where the data User2 is in the same row. I know that I can find an a-tag with partial link like findElement(By.partialLinkText("/ResetPassword/")); (the number 2 can change, so I canĀ“t use it as seperator). But I need to seperate it by User. Is there a solution like tr.td.text("User2") > findElement(By.partialLinkText("/ResetPassword/"));?
This XPath should do the trick for you. .//tr[td[normalize-space(text())='User2']]//a Just keep changing "User2" part with the desired user value.
Hi I was wondering if you could use the .getValue() statement with /#a at the end of the xpath to locate the attribute "a".
Main thing to do is find a bulletproof xpath to locate the User2 row once you have done that finding the value of "a" should be easy enough.
I hope this helps
U can try something like this(not sure though)-
List<WebElement> list=table.findElements(By.tagName("tr"));
List<WebElement> tdvalues=null;
for(WebElement web:list){
tdvalues=web.findElements(By.tagName("td"));
if(tdvalues.contains("User2")){
System.out.println(tdvalues.get(0).getText());//0th position contains the link
}
tdvalues.clear();
}
I want to select this table:
<table class="pane sortable" id="game.scores">
<tbody>
<tr>
.....
</table>
I tried to do this:
Elements el = doc.select("table#game.scores");
System.out.println(el.html());
But it does not work.
Try this selector,
table[id=game.scores]
You can verify various selectors without running java code, here
I am looking for the help to read a table span using selenium code and write span into the file,
following is my html code
<table>
<tbody><tr>
<tr>
<td><span>
FIRST
</span>
</td>
</tr>
<tr>
<td>
<span>SECOND</span>
</td>
</tr>
<tr>
<td>
<span>THIRD</span>
</td>
</tr>
<tbody>
</table>
I need to write FIRST SECOND THIRD on a file in java.
Thanks a lot.
I suppose you have located/found the table WebElement. Then you can get span elements content like this:
List<WebElement> spanElements = tableElement.findElements(By.ByTagName("span"));
for (WebElement element : spanElements) {
String spanContent = element.getText();
//save it to a collection or a StringBuilder, then write it to a file
}
Having a look at this and this might help.
1) XPath that gets all text is:
//span/text()
2) in java code you may type something like
String text = selenium.getText("xpath=//span");
I have a html table like:
<table ..... class="cars">
<tr class="item-odd">
...
</tr>
<tr class="item-even">
</tr>
</table>
How could I get the table rows using xpath?
//tr[contains(#class, ???)
can I use OR in there somehow to say item-odd|item-even
You can get the rows like this:
//table[#class='cars']/tr
To get the item-odd rows, you would do this:
//table[#class='cars']/tr[#class='item-odd']
Yes, you can use or, e.g.:
//table[#class='cars']/tr[#class='item-odd' or #class='item-even']
See http://www.w3.org/TR/xpath for more details.