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 get 34,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).getText();
But on console it gives error as:
Exception in thread "main" java.lang.NullPointerException
4 Answers 4
Your example code is missing a "
and a )
in the By.xpath
. Guess you would get a syntaxError so your real code might be correct.
Do you wait for the element, because I think the findElement
returns no object ( e.g. NULL) when it is not displayed (yet) and called getText()
on a NULL is not possible , resulting in the NullPointerException
. Read: https://stackoverflow.com/questions/20903231/how-to-wait-until-an-element-is-present-in-selenium
Keep in mind findElement does not wait for the element, it just fails instantly.
I think the there is a problem with the provided XPath, you are navigating to the paragraph tag (<p> tag)
and fetching the text, I suggest you navigate to the bold tag (<b>tag)
and fetch the respective value, like the following:
String count = driver.findElement(By.xpath("//div[@class='alert alert-count']/p/b")).getText();
-
I think this should workSagar007– Sagar0072017年05月02日 09:00:33 +00:00Commented May 2, 2017 at 9:00
-
Are you sure? In the C# bindings, the Text property gets ALL innerHTML, even from child nodes.FDM– FDM2017年05月02日 09:51:43 +00:00Commented May 2, 2017 at 9:51
-
@FDM What you have said is also true. As Neils, Mentioned the syntax error may also be the casethe_coder– the_coder2017年05月03日 03:42:51 +00:00Commented May 3, 2017 at 3:42
We are providing test automation services to clients and we have tried the below code for getting text of HTML tag:-
String count = driver.findElement(By.xpath("//div[@class='alert alert-count']/p).getAttribute("innerhtml"); //getting text from HTML
System.out.println(count); //printing the text
-
DIDNT close bracket and double quotes generates error 'WebElement' object has no attribute 'findElement' this will generate Attribute error replace it with driver.find_element_by_xpath("//div[@class='alert alert-count']/p").getAttribute("innerhtml")dataninsight– dataninsight2022年01月16日 07:15:30 +00:00Commented Jan 16, 2022 at 7:15
you can get the text for an HTML tag using the below snippet
String count = driver.findElement(By.xpath("//div[@class='alert alert-count']/p).getAttribute("innerhtml");
System.out.println(count);
It will fetch the text inside the HTML and prints it to console.
-
missing closing double quotes and bracket this will generate error : String count = driver.findElement(By.xpath("//div[@class='alert alert-count']/p).getAttribute("innerhtml");dataninsight– dataninsight2022年01月16日 06:42:40 +00:00Commented Jan 16, 2022 at 6:42