I have a section of code like this:
<div id='wrapper'>
<span>*</span>
Question 1
</div>
I want to store the Question 1 as a string in Java so when I use the xpath //div/text()
to get the text "Question 1." However when I try to print the value, I am getting the error Invalid Selector Exception Xpath //div/text() is: [object Text]. It should be an element.
String text = driver.findElement(By.xpath("//div")).getText();
//returns *Question 1
String text = driver.findElement(By.xpath("//div/text()")).getText();
//returns error
How am I supposed to store just the Question 1. I can replace the * once I get it using the first method above but I don't want to do that.