WebDriver getText() Method with replace using Java - java

Html Code
<span data-bind="html: TotalCharges">
<span class="CurrencySymbol">USD </span>
7400.00
<br>
(0.00+0.00)
</span>
Webdriver to get the Totalcharge value using getText method
Code:
driver.findElement(By.xpath("//span[#data-bind='html: TotalCharges']")).getText().substring(4);
with above will get the below output
"7400.00
(0.00+0.00)"
my Expected output :"7400.00"
so how can i replace the char from "< br>" tag (need to replace "(0.00+0.00)")
i'm using java

Use the following xpath to get 7400.00:
driver.findElement(By.xpath("//span[#class='CurrencySymbol']/following-sibling::text()[1]").getText();
Oh My mistake, Thanks for correcting me #alecxe:
You can get it by:
driver.findElement(By.xpath("//span[#class='CurrencySymbol']/.."))
.getText().split("\n")[0].split(" ")[1]
splitting at \n will split it for <br> tag.

Try following solution. It will give you 7400.00 output-
String temp = driver.findElement(By.cssSelector("html>body>span")).getText();
String s1=temp.replace("USD", "").replace("\n", "").replace("\r", "");
String finalStr = s1.substring(0,s1.indexOf("(")).trim();
System.out.println(finalStr);

Related

Android remove a class inside div

i'm trying to get the text in the reference class div without getting the text in the inner div.
I just want what's inside a <div class="class1"> -> "123456789" without taking "abcdefg".
<div class="class0">
<div class="class1">
<div class="class2">
abcdefg
</div>
123456789
</div>
</div>
i tried to run this but it always takes the text i don't want
String text = doc.getElementsByClass("class1").html();
String text2 = text.replaceAll("</?div[^>]*>","");
Log.d("text2", text2 );
output:
abcdefg
123456789
but I just want 123456789
how can I do? thank you all
i solved now by myself
doc.select("div.class2").first().remove();
String text = doc.getElementsByClass("class1").html();
Try
text = document.getElementsByClassName("class1");
text[0].outerText.split('\n')[1];

Get Text with Selenium Webdriver in Java

I need to extract text Paul Robert this html and print to the console using the Java WebDriver and selenium .
The code below extracts all text Div , even the text " Quit".
<div role="alert" class="alert success" id="control_gen_3">
<p>
<strong>Invite to Paul Robert.</strong></p>
<button class="dismiss" id="global-error-dismiss">Quit</button>
</div>
Code Selenium:
String pessoa = driver.findElement(By.id("control_gen_3")).getText();
System.out.println(pessoa );
String pessoa = driver.findElement(By.xpath("//div[#id='control_gen_3']//a")).getText();
System.out.println(pessoa );
Hope this helps you..
String pessoa = driver.findElement(By.cssSelector("#control_gen_3 a")).getText();
System.out.println(pessoa);
This means you fetch the element with id control_gen_3 and look for an a within that element.

WebDriver getText() Method with Replace <br> Tag

Html Code
<td class="left-align no-wrap-clmn">
<span style="color:orange">
In
<br>
Process
</span>
</td>
Webdriver to get the In Process value using getText method
Code:
String status = driver.findElement(By
.xpath("//*[#id='tblPortfolio']/tbody/tr[" + row
+ "]/td[13]")).getText();
for the Above code Return the value with Following format
Output for the above
In
Process
after that i will pass the above value with IF Statement
Code:
if (status.equals("In Process") {
}
issue:
its not get into the IF Statment, because of the new Line char
so how can i make an output with "In Process" (with my getText()) or any changes in IF Statement
You need to remove the new line character, try this :
status.replaceAll("\\r\\n|\\r|\\n", " ");
It will remove all line breaks with space.

Add a new html tag to an html string in android

I have a string obtained from an EditText. The string contains html tags.
Spannable s = mainEditText.getText();
String webText = Html.toHtml(s);
The contents of the string is :
<p dir="ltr">test</p>
<p dir="ltr"><img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-0de7a730-3fa9-4a1e-9f82-d34e4f6e2d31-file" /><br /></p>
<p dir="ltr"><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C22.572646,88.363895" /><br /> </p>
Now, what I want to do is, wherever there is an img src tag, I want to precede it with a center tag.
What should I do to get the following output?
<p dir="ltr">test</p>
<p dir="ltr"><center><img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-0de7a730-3fa9-4a1e-9f82-d34e4f6e2d31-file" /></center><br /></p>
<p dir="ltr"><center><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C22.572646,88.363895" /></center><br /> </p>
Can a regex solve the issue or should it be done in a different way?
Can JSOUP help in any way? Is there any other type of HTML parser which can do the job?
(<img\s+[^>]*>)
You can try this.Replace with <center>$1</centre>.See demo.
http://regex101.com/r/sU3fA2/38
Something like
var re = /(<img\s+[^>]*>)/g;
var str = '<p dir="ltr">test</p> \n<p dir="ltr"><img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-0de7a730-3fa9-4a1e-9f82-d34e4f6e2d31-file" /><br /></p> \n<p dir="ltr"><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C22.572646,88.363895" /><br /> </p>';
var subst = '<center>$1</centre>';
var result = str.replace(re, subst);
By using Jsoup, you can use the wrap() method of the Element class of Jsoup.
It would look like this :
public String wrapImgWithCenter(String html) {
Document doc = Jsoup.parse(html);
doc.getElementsByTag("img").wrap("<center></center>");
return doc.html();
}
I implemented the JSOUP solution suggested by mourphy. But, I had edited the method a little and it did the miracle for me. The new method is:
public String wrapImgWithCenter(String html){
Document doc = Jsoup.parse(html);
doc.select("img").wrap("<center></center>");
return doc.html();
}
Thanks mourphy and vks for your help!
Using Regex, you could also do this in java:
String formatted = str.replaceAll("(<img\\s+[^>]*>)", "<center>$1</center>");

Get part of string that is not html in Java

In my Java application I have String that have to be edited. The problem is that these Strings can contain HTML tags/elements, which should not be edited (no id to retrieve element).
Scenario (add -):
String a = "<span> <table> </table> </span> <div></div> <div> text 2</div>";
should become: <span> <table> </table> </span> <div></div> <div> -text 2</div>
String b = "text";
should become: -text
String c = "<p> t </p>";
should become: <p> -t </p>
My question is: How can I retrieve the text in a string that can contain html tags (cannot add id or class)
You can use an XML parsing library.
String newText = null;
for ( Node node : document.nodes() ) {
if ( node.text() != null ) newText = "-" + node.text();
}
note that this is pseudo.
newText will now be -text or whatever the node text is.
EDIT:
Your question is a bit ambiguous in terms of "the text can contain html elements."
If it doesn't contain html tags, then you cannot use an XML parser, which brings up the question.. if it doesn't contain tags, then why can't you just do...
String newString = "-" + a;

Categories

Resources