问题
I am aware of .getAttribute("innerHTML"), which is one of the ways to retrieve the value of h1 tag but my HTML looks like this:
I can reach h1 tag but not able to reach innerHTML.
I want to retrieve text from innerHTML using selenium WebDriver in Java
Solution I used :
First I located h1 tag using
@FindBy(xpath = "//*[@id=\"main\"]/h1") with element name as Findelement, then used Findelement.getAttribute("[0].innerHTML") to retrieve text but while running program is throwing java.lang.NullPointerException.
回答1:
Found out the solution. I used the below code to retrieve the text.
JavascriptExecutor js = (JavascriptExecutor)driver;
String sText = ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML;",driver.findElement(By.xpath("//*[@id=\"main\"]/h1"))).toString();
来源:https://stackoverflow.com/questions/53599936/selenium-java-text-retrival-from-h1-tag