Selenium webdriver 使用Selenium从脚本标记获取javaScript代码:这可能吗?

Selenium webdriver 使用Selenium从脚本标记获取javaScript代码:这可能吗?,selenium-webdriver,Selenium Webdriver,我从下面代码的最后一行得到一个错误 // Scala code val driver: HtmlUnitDriver = new HtmlUnitDriver() val el = driver.findElementByXPath(xp) val js = el.getText() 返回的“el”元素是一个“script”标记。我想知道是否可以使用.getText()获取标记的内容 这是错误 Exception in thread "main" java.util.NoSuchElement

我从下面代码的最后一行得到一个错误

// Scala code
val driver: HtmlUnitDriver = new HtmlUnitDriver()
val el = driver.findElementByXPath(xp)
val js = el.getText()
返回的“el”元素是一个“script”标记。我想知道是否可以使用.getText()获取标记的内容

这是错误

Exception in thread "main" java.util.NoSuchElementException: None.get

就我所见,不可能使用XPath访问“script”元素的内容。我找到的解决方案是在HTML源代码上应用正则表达式:

// Scala code
val regex = "your (pattern)".r
val res = ( regex findFirstMatchIn driver.getPageSource() ).get group 1

是的,您可以通过使用类似el.getAttribute(“innerHTML”)的属性来获取脚本的内容