How would I be able to use JSoup to get the data-code value from a table row?
Here is what I have tried but it just prints nothing:
Document doc = Jsoup.connect("http://www.example.com").get();
Elements dataCodes = doc.select("table[class=team-list]");
for (Element dataCode : dataCodes)
{
System.out.println(dataCode.attr("data-code"));
}
The HTML code looks like this:
<body>
<div id-=""main">
<div id="inner">
<div id="table" class="scores-table">
<table class ="team-list">
<tbody>
<tr data-code="1" class="data odd"></tr>
<tr data-code="2" class="data even"></tr>
<tr data-code="3" class="data odd"></tr>
<tr data-code="1" class="data even"></tr>
</tbody>
</table>
</div>
</div>
</div>
I want it to print out the data-code value on each row of the table (which is the team number).
Your selector should go down to tr elements:
Elements dataCodes = doc.select("table.team-list tr");
According to the comments, this still results into an empty list - in this case, the table is probably dynamically generated with the help of javascript logic or a separate AJAX request.
In this case, one of the possible approaches would be to have a real browser handle that dynamic javascript, AJAX part. Try selenium browser automation framework:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list tr"));
for(WebElement element: elements)
{
System.out.println(element.getAttribute('data-code'));
}
Related
<tr class="odd" role="row">
<td>PRODTUT020</td>
<td>Product for tours</td>
<td>
<td>
<ul class="icons-list">
<c:if test="true">
<li class="text-info-600">
<a href="edit-se-product_mst-90" title="View">
<i class="icon-eye"/>
</a>
</li>
</c:if>
</ul>
</td>
</tr>
Above is the html body. Want to click on the icon-eye element. How do we identify this element using selenium locators?
This XPath,
//tr[td="PRODTUT020"]//i[#class="icon-eye"]
will select i elements with #class attribute value of icon-eye beneath the tr element whose td child has a string value of PRODTUT020.
Depending upon what's more invariant across your general cases, you might change PRODTUT020 to Product for tours -- both work for the case you show.
It avoids having to name the namespaced element by skipping past it via //.
If you want to fetch i inside c:if element you can use below expression
'//*[name()="c:if" and #test="true"]//i'
How do I make sure that my selenium code scrapes all matching contents of my XPath?
Please help me with your ideas.
For example, these are my HTML Tags:
<tr class="1" role="r1">
<td class="c1">
<a href="www.google.com">
</a>
</td>
</tr>
<tr class="2" role="r2">
<td class="c2">
<a href="www.youtube.com">
</a>
</td>
</tr>
<tr class="3" role="c3">
<td class="c3">
<a href="www.facebook.com">
</a>
</td>
</tr>
I want my selenium code to fetch all links from href tag.
So, below is my XPath:
String links = driver.findElement(By.xpath("//tr[#role='cad']//td[#class='c1']//a")).getAttribute("href");
System.out.println(links);
but it fetches only the first href output, i.e. www.google.com.
The desired output is:
www.google.com
www.youtube.com
www.facebook.com
How can I achieve this?
Any array implementation would be better options?
Try Following code:
List<WebElement> elements= driver.findElements(By.xpath("//table/tbody/tr"));
int i =0 ;
while(i<elements.size()){
WebElement childElement = elements.get(i).findElement(By.cssSelector("a"));
System.out.println(childElement.getAttribute("href"));
i++;
}
Try below code.
List<WebElement> links = driver.findElements(By.xpath("//tr/td/a"));
for(int i=0;i<links.size();i++){
System.out.println(links.get(i).getAttribute("href"));
}
I'm newby in selenium automation.
I have one scenario where I have to click on a button based on some title. but the button element looking same for all all rows.
This is the image:
this is html for the same-
<tr class="odd gradeX">
<td class="hide"></td>
<td>My sample Test</td>
<td> Priyank pareek </td>
<td style="width:50px;text-align:center;">01/11/2017</td>
<td style="width:50px;text-align:center;"> Approved </td>
<td style="width:50px;text-align:center;">
<td style="width:100px;">
<a class="btn btnReject red btn-sm btn-outline sbold uppercase" href="javascript:;" onclick="updateTest(this)" data-status="0" data-gui="6438C99F-E166-49DA-8B89-DD0E2EF33A62" title="Reject">
</td>
</tr>
How can i click on the reject button ? please help me
You can use below XPath:
WebElement element = driver.findElement(By.xpath("//a[#title='Reject']"));
or CSS selector
WebElement element = driver.findElement(By.cssSelector("a[title='Reject']"));
If these selectors still matches multiple elements, try:
WebElement element = driver.findElement(By.xpath("//tr[td[text()='My sample Test']]//a[#title='Reject']"));
Try to locate using following xpath -
//td[text()='My sample Test']/following-sibling::td/a[#title='Reject']
Explanation :-
Find the td tag which having text as 'My sample Test'
following-sibling used to navigate the sibling which have reject button
You can search using class name btnReject. In Python, I usually use driver.getElementByClassName("btnReject").click(). You can change them into Java to use.
I have an HTML page containing the following code :
<table class="report" style="width:100%">
<tbody>
<tr>
<th/>
<th>Position Open
<br>
<span class="timestamp">27/7/2016 16:12:12</span>
</br>
</th>
<th>Position closed
<br>
<span class="timestamp">27/7/2016 16:12:42</span>
</br>
</th>
</tr>
<tr>
<td>
<span dir="ltr">EURJPY</span>
</td>
<td>116.098</td>
<td>116.156</td>
</tr>
</tbody>
</table>
On this page I have another table with the same class attribute "report" but only this table contains texts "Position Open" and "Position Closed".
I need to select elements containing the "EURJPY", "116.098" and "116.156" data.
These elements content is changing i.e. instead of "EURJPY" may appear "EURUSD" or "GBPCAD" etc.
I tried the following code:
driver.findElement(By.xpath("//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span")).getAttribute("textContent");
to get the first required field text but got the Invalid selector error.
Your XPath is close but there were a couple issues.
//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
You are searching for a SPAN that contains the text 'Position Open' when in fact it is a TH that contains the text.
//th[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
(//tr) should be corrected to //tr
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td/span
What you want is the text contained in the TD, not the SPAN. If you pull the text from the TD you can get the text you want from all three elements. If you pull the SPAN, then you will also need to pull the last two TDs. This way is just simpler.
...and finally, the TH contains more than just the text you are looking for. Use .contains() to get a match.
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td
So we take that XPath and put it into Java code and we get the below.
List<WebElement> tds = driver.findElements(By.xpath("//th[contains(text(),'Position Open')]/ancestor::table[#class='report']//tr[2]/td"));
for (WebElement td : tds)
{
System.out.println(td.getText());
}
There can be issues matching the text sometimes, use contains instead, try this selector
//th[contains(.,'Position')]/ancestor::table[#class='report']//tr[2]/td/span
You can use this xpath to locate the 3 <td> tags you are interest in
//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td
Using it will give you list of three elements, you can extract the text from them
List<WebElement> tds = driver.findElement(By.xpath"//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td");
String currency = tds.get(1).getText(); // this will be EURJPY
tds.get(2).getText(); // 116.098
tds.get(3).getText(); // 116.156
I need to select testdt4 from the table. I am not able to get the whole list of the table items displayed as web elements. Like testdt4, there are many rows of the table which i need to find. Can I get the whole list in List<WebElement>? Please help.
<tr class="ng-scope" ng-class="::{warning: !$$cancelLastAction && (entity.$$inProgress || entity.$$hasError)}" ng-repeat="entity in locals.data">
<td class="selectRow">
<!-- ngRepeat: columnDef in locals.gridOptions.columnDefs -->
<td class="ng-scope" ng-repeat="columnDef in locals.gridOptions.columnDefs">
<ci-resource-grid-cell text-limit="textLimit" column="columnDef" data="entity">
<a class="ng-scope" ng-click="openLink(entity)" href="">
<div class="ciTruncate" truncate-limit="150" ci-truncate="columnDef.$$parse(entity)">
<span class="ng-scope">**testdt4**</span>
</div>
</a>
</ci-resource-grid-cell>
</td>
<!-- end ngRepeat: columnDef in locals.gridOptions.columnDefs -->
<td class="ng-scope" ng-repeat="columnDef in locals.gridOptions.columnDefs">
First - you need a established goal as to what you are trying to find. Are looking for all elements or Single element. If multiple elements.
[https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/SearchContext.html#findElements-org.openqa.selenium.By-]
are you trying to find Cells or Rows or whole table? Cell tag - td/Row - tr/Table - table.
with find elements, xpath search with following will give you all cells:
Xpath Expression::
"//td//span[contains(#class,"ng-scope") and contains(text(),"testdt4")]"
Where I gave //tr above, replace with table or tr as needed [pt2]
Example:
Xpath Expression::
"//tr//span[contains(#class,"ng-scope") and contains(text(),"testdt4")]"
Xpath Expression::
"//table//span[contains(#class,"ng-scope") and contains(text(),"testdt4")]"
List someList = driver.findElement(By.xpath("Xpath Expression"))
Show me your code i will modify it:: Update You should be using TD for cells.
By identifierXPath = By.xpath("//tr//span[contains(#class,'ng-scope') and contains(text(),'testdt4')]");
List<WebElement> list_Cells = driver.findElements(identifierXPath);
System.out.println(list_Cells.size());
for (WebElement single_Cell : list_Cells){
System.out.println("Cell Text ::-" + single_Cell.getText().trim());
}