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.
Related
Hi I could not get the text from html I wanna get this text This is a test text
<div class="rehou">
<span class="tlid-t t">
<span title="" class="">This is a test text</span>
</span>
<span class="tlid-t-v" style="" role="button"></span>
</div>
My java:
Document doc = Jsoup.connect(url).get();
Elements ele= doc.select("span.tlid-t t");
textass = ele.text();
The span has the two different classes tlid-t and t. So if you want to use both classes in your select you should use span.tlid-t.t instead of span.tlid-t t.
Elements ele = doc.select("span.tlid-t.t");
String textass = ele.text();
System.out.println(textass);
Which would print This is a test text.
But this will select the outer span! If the html gets changed the content of textass will be also changing. If you only want to select the text of the inner span you should use span.tlid-t.t span.
Elements ele = doc.select("span.tlid-t.t span");
String textass = ele.text();
System.out.println(textass);
This will also print This is a test text.
Please find below the sample HTML code:
<div class="form-element email-address" id="gmail-address-form-element">
<label id="gmail-address-label">
<strong>
Choose your username
</strong>
<input type="text" maxlength="30" autocomplete="off"
name="GmailAddress" id="GmailAddress" value=""
spellcheck="false">
<span class="atgmail">#gmail.com</span>
</label>
I need to verify "Choose your username" label is bold with Selenium WebDriver.
Appreciate if the language used is java.
You can use the getComputedStyle to get the style of the element.
Use font-Weight to check if it is bold or not
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.cssSelector("#gmail-address-label > strong"));
String fontWeight = (String) js.
executeScript(
"return getComputedStyle(arguments[0]).getPropertyValue('font-Weight');",
element);
if (fontWeight.trim().equals("bold")) {
System.out.println("Is Bold");
} else {
System.out.println("Not Bold - " + fontWeight);
}
In simple way we can get the CssValue of font-weight and check if it is 'bold' or is greater than or equal to 700 or is 'bolder' .Because by all these ways the bold style can be given to the text
Use following code in java
String fontWeight = driver.findElement(By.className("classname"))
.getCssValue("font-weight");
boolean isBold = "bold".equals(fontWeight) || "bolder".equals(fontWeight) || Integer.parseInt(fontWeight) >= 700;
Hope it will help.
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);
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>");
I have been trying to get the anchor link via WebDriver but somehow, things aren't working as desired and I am not getting the element.
Below is the HTML Structure:
<div id="1">
<table width="100%">
<tr>
<td>.....</td>
<td>
<ul class="bullet">
<li>....</li>
<li>....</li>
<li>
myText
</li> // myText is the text I am searching for
</ul>
</td>
</tr>
</table>
<div>....</div>
</div>
The <li> elements contains anchor tags with links only. No id or any other attribute they contain. The only difference is the text displayed by them and hence, I am passing myText to detect exactly what I need.
And for this, the java code I have been trying is:
driver.get("url");
Thread.sleep() //waiting for elements to get loaded. Exceptional Handling not done.
WebElement divOne = driver.findElement(By.id("1"));
WebElement ul = divOne.findElement(By.className("bullet"));
WebElement anchor = null;
try{
anchor = ul.findElement(By.partialLinkText("myText"));
}catch(NoSuchElementException ex){
LOGGER.warn("Not able to locate tag:" + linkText + "\n");
}
String myLink = anchor.getAttribute("href"); // null pointer exception
I don't understand why is this happening. What is the correct way to do this? Should I use some other method?
driver.findElement(By.xpath("//a[contains(text(),'myTest')]"));
that searches for myTest text. I haven't tried the code, I hope it helps you
you can use any of the below. Should work for you
List<WebElement> anchor = driver.findElements(By.partialLinkText("myText"));
or
driver.findElements(By.linkText("myText"))
or
driver.findElements(By.Xpath("//a[contains(text(),'myText')]"));
String myLink = anchor.getAttribute("href"); // null pointer exception
You are getting exception because you didn't set the anchor value, anchor variable is created and intialized to null and after that it is never updated.
I think the correct code should be below
String myLink = element.getAttribute("href");
Try using a WebDriver instance directly instead of WebElement instance...i.e., use driver instead of ul..
you can try with wait,
element = new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("myText")));