Selenium Java : Text retrival from h1 tag

馋奶兔 提交于 2021-01-29 14:32:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!