how to get value of text wrapped in paragraph element <p> with selenium in java
18,342
Solution 1
Try using following:
String count = driver.findElement(By.cssSelector("div.alert.alert-count p")).getText();
Solution 2
Try this below code using xpath locator
String element = driver.findElement(By.xpath("//div[@class='alert alert-count']/p/b")).getText();
System.out.println(element);
Explanation of xpath:- Use class
attribute of <div>
tag and move ahead with <p>
and <b>
tag.
Author by
Prasad_Joshi
Updated on June 05, 2022Comments
-
Prasad_Joshi almost 2 years
I have a button, clicking on it generates a number which is wrapped in paragraph text such as
<p>random number <p>
, I want to get that random number value and do operations based on number it generates. From below I need to get34,756
number and store it in java here is the html code for it:<div class="form-group"> <div class="alert alert-count"> <p> <b> <!-- react-text: 531 --> <!-- /react-text --> <!-- react-text: 532 --> 34,756 <!-- /react-text --> </b>
and the xpath I used is as below
String count = driver.findElement(By.xpath("//div[@class='alert alert-count']/p[1]/b).getText();
but on console it gives error as
Exception in thread "main" java.lang.NullPointerException
-
Fran Montero about 7 yearsIs
By.xpath("//div[@class='alert alert-count']/p)
working? -
Prasad_Joshi about 7 yearsyes..it is working correctly, only problem is to get that value in a string
-