Remove table rows for given pattern in Java String - java

I want to remove all the rows having N/A as value is the last column of given html code (java string).
Please help me in getting correct regex/pattern code to remove all occurrences:
<table class="overviewTable">
<tr>
<th colspan="6" class="header suite">
<div class="suiteLinks">
Groups
</div>
Test Automation
</th>
</tr>
<tr class="columnHeadings">
<td> </td>
<th>Duration</th>
<th>Passed</th>
<th>Skipped</th>
<th>Failed</th>
<th>Pass Rate</th>
</tr>
<tr class="test">
<td class="test">
Test Xyz
</td>
<td class="duration">
0.000s
</td>
<td class="zero number">0</td>
<td class="zero number">0</td>
<td class="zero number">0</td>
<td class="passRate">
N/A
</td>
</tr>
<tr class="test">
<td class="test">
Test abc XYZ
</td>
<td class="duration">
0.000s
</td>
<td class="zero number">0</td>
<td class="zero number">0</td>
<td class="zero number">0</td>
<td class="passRate">
N/A
</td>
</tr>
<tr class="test">
<td class="test">
TestAbcSuccess
</td>
<td class="duration">
77.582s
</td>
<td class="passed number">1</td>
<td class="zero number">0</td>
<td class="zero number">0</td>
<td class="passRate">
100%
</td>
</tr>
<tr class="suite">
<td colspan="2" class="totalLabel">Total</td>
<td class="passed number">1</td>
<td class="zero number">0</td>
<td class="zero number">0</td>
<td class="passRate suite">
100%
</td>
</tr>
</table>
This is index.html file of Java+Selenium+TestNG automation results.
Please help me in getting correct regex/pattern code to remove all occurrences from the above HTML.
These are my trials:
1.
fullHtmlStr = fullHtmlStr.replaceAll("(?<=<tr class=\"test\">).*?(?=N/A\n </td>)", "");
2.
Pattern PATTERN = Pattern.compile("<tr class=\"test\">.*$.N/A\n </td>", Pattern.MULTILINE | Pattern.DOTALL );
Matcher m = PATTERN.matcher(fullHtmlStr);
if (m.find())
fullHtmlStr = m.replaceAll("");
(I don't have any knowledge of regex, so please forgive my incase these are totally wrong)
Attaching screenshots:
Screenshot Before Updation:
Screenshot After Updation:

I discourage you to use a regexp to do this matter: Regular expressions are useful to match patterns made of characters, but not patterns made of patterns.
To process an HTML string you need a proper parser: If it is XHTML, you can parse it straightforward through a DocumentBuilder. If not, you need to convert it first to XHTML through opensource library Tidy.
Through a parser you'll convert your HTML string to a Document object, which you shall process to traverse it, add or remove any nodes.

Based on the suggestion by #little-santi, i have used jsoup html parser to manipulate the code, here is my code:
Document document = Jsoup.parse(strText);
for( org.jsoup.nodes.Element element : document.select("td:eq(5)")) {
String content = element.getElementsMatchingOwnText("N/A").text();
if(content.equalsIgnoreCase("N/A")) {
element = element.parent();
element.remove();
}
}
strText = document.toString();

Related

Spring Boot, Thymeleaf Form Error => i want edit line selected from table

I want to edit a row of a table in another html page.
I have a beginner with thymeleaf and Spring MVC...
profilesAll.html
but, i have this error :
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputRadioFieldTagProcessor' (template: "profilsAll" - line 39, col 26)
....
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'profil' available as request attribute
ProfilsAll.html :
<form th:action="#{/profiles/update}" th:object="${profil}" method="POST">
<table border="1">
<thead>
<tr>
<th>Select</th>
<th>UID</th>
<th>SIP</th>
<th>enterpriseVoiceEnabled</th>
<th>voicePolicy</th>
<th>dialPlan</th>
<th>samAccountName</th>
<th>exUmEnabled</th>
<th>exchUser</th>
<th>objectClass</th>
<th>statusProfile</th>
</tr>
</thead>
<tbody>
<tr th:if="${skypeProfiles.empty}">
<td colspan="2"> No skype profile available </td>
<tr th:each="skypeProfile, profile:${skypeProfiles}">
<td>
<input type="radio" th:field="*{profile}" th:value="${profile}" />
</td>
<td th:text=${skypeProfile.collaboraterId}>UID</td>
<td th:text=${skypeProfile.SIP}>SIP</td>
<td th:text=${skypeProfile.enterpriseVoiceEnabled}>enterpriseVoiceEnabled</td>
<td th:text=${skypeProfile.voicePolicy}>voicePolicy</td>
<td th:text=${skypeProfile.dialPlan}>dialPlan</td>
<td th:text=${skypeProfile.samAccountName}>samAccountName</td>
<td th:text=${skypeProfile.exUmEnabled}>exUmEnabled</td>
<td th:text=${skypeProfile.exchUser}>exchUser</td>
<td th:text=${skypeProfile.objectClass}>objectClass</td>
<td th:text=${skypeProfile.statusProfile}>statusProfile</td>
</tr>
</tbody>
</table>
<button type="submit" value ="Edit">Edit</button>
</form>
Controller :
#PostMapping("profiles/update")
public String profilesUpdate(#ModelAttribute("profil") SkypeProfileSearchBean skypeProfile) {
System.out.print("skypeProfile id "+skypeProfile.getCollaboraterId());
return "profilsUpdate";
}
This code is problematic but I did not find the solution :
<input type="radio" th:field="*{profile}" th:value="${profile}" />
Thank you very much for your help
You don't need to have two instances of skypeProfiles to use one in object and display one. Just having one instance would be enough.
Ref - https://www.baeldung.com/spring-boot-crud-thymeleaf
replace <tr th:each="skypeProfile, profile:${skypeProfiles}">
with <tr th:each="skypeProfile : ${skypeProfiles}">
and <input type="radio" th:field="*{profile}" th:value="${profile}" />
with <input type="radio" th:field="*{skypeProfile}" th:value="${skypeProfile}" />

Thymeleaf - Click able URL (Web link) in Table

(I have looked on google, here.. and havent been able to find the answer that could help me).
Goal : Website to be entered as a click able web link
- Link is retrieved from DB as a varchar(100)
Tried all kinds of different solutions.. without any luck - Maybe one of you guys can help me out ? :). thank you in advance.
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Zip</th>
<th>Website</th>
<th>Email</th>
<th>Phone</th>
<th>Location</th>
<th>Agelimit</th>
</tr>
</thead>
<tbody>
<tr th:each="sps: ${sp}">
<td th:text="${sps.venueId}"/>
<td th:text="${sps.name}"/>
<td th:text="${sps.address}"/>
<td th:text="${sps.city}"/>
<td th:text="${sps.zip}"/>
<td th:text="${sps.website}"/>
<td th:text="${sps.email}"/>
<td th:text="${sps.phone}"/>
<td th:text="${sps.location}"/>
<td th:text="${sps.ageLimit}"/>
</tr>
</tbody>
</table>
You can use any type of tag with Thymeleaf. Is this what you want?
<td>
<a th:href="${sps.website}" th:text="${sps.website}" target="_blank" />
</td>

How to select a link of a Record in the table contains multiple Rows of different class with same class name for each column

<table class="table hover" id="resultTable">
<thead>
<tr>
<th rowspan="1" class="checkbox-col"><input type="checkbox" id="ohrmList_chkSelectAll" name="chkSelectAll" value=""></th>
<th rowspan="1" style="width:400" class="header">Report Name</th>
<th rowspan="1" style="width:95"><span class="headerCell"></span></th>
<th rowspan="1" style="width:95"><span class="headerCell"></span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><input type="checkbox" id="ohrmList_chkSelectRecord_5" name="chkSelectRow[]" value="5"></td> <td class="left">PIM Sample Report</td>
<td class="left">Run</td>
<td class="left">Edit</td>
</tr>
<tr class="even">
<td><input type="checkbox" id="ohrmList_chkSelectRecord_6" name="chkSelectRow[]" value="6"></td> <td class="left">Sample Report</td>
<td class="left">Run</td>
<td class="left">Edit</td>
</tr>
</tbody>
</table>
I'm a beginner to selenium, so I need a help to click a link (Run)of a Record from multiple Records in the table contains multiple Rows of different class with same class name for each column. I have attached the code snippet.
I tried this Xpath but is not working:
//table[#class='table hover']/tbody/tr/td[text()='PIM Sample Report'] and ./td[text()='Run']
can someone help me to figure out this?
Try to use below XPath :
//td[.="PIM Sample Report"]/following-sibling::td[.="Run"]/a
This should fetch the link "Run" preceded by td with text "PIM Sample Report"

Check a valeur with selenium

I want to verify that i have a trafic in my box. So i want to check that "Total" is different de 0 but i fail.
http://clip2net.com/s/54bx8J
HTML code :
<span>
<table>
<tbody>
<tr>
<td class="left">Upload</td>
<td class="right">276 KB</td>
</tr>
<tr>
<td class="left">Download</td>
<td class="right">661 KB</td>
</tr>
<tr>
<td class="left">Total</td>
<td class="right">937 KB</td>
</tr>
<tr>
<td class="left">Duration</td>
<td class="right">5min19s</td>
</tr>
</tbody>
</table>
</span>
And my script :
WebElement data = (driver.findElement(By.xpath("//tr[#class='right' and text()[contains(., 'Download')]/td[2]")));
System.out.println(data);
Thread.sleep(3000);
assertTrue(data != null);
Thanks
You should not try to print the WebElement. Instead, try to access its data by calling getAttribute or getText, depending on the type of element you are selecting.
See http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html for the WebElement spec.
Also, it would be helpful if you post the html source that you are trying to select/parse.

How to pass data from selected rows using checkboxes from JSP to the server

I'd like to know if there's any way to send data to the server for the selected rows using the checkboxes I've put on those rows? I mean , how can I send only the data of those selected rows to the server?
Here's the html code I use:
<table>
<thead>
<tr class="tableheader">
<td width="10%"></td>
<td width="30%">Vehicle</td>
<td width="40%">Driver</td>
<td width="10%">Mileage</td>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectedItems"
value="c:out value="${item.numberPlate}"/>"/>
</td>
<td align="left"><c:out value="${item.numberPlate}"/></td>
<td align="left"><c:out value="${item.driver.fullName}" /></td>
<td align="left"><input type="text" name="mileage" value="" /></td>
</tr>
</c:forEach>
</tbody>
</table>
I really hope you can give some guidance on this.
Thanks in beforehand.
When handling the request use request.getParameterValues("selectedItems"). And I don't think it's needed to add [] to names

Categories