【问题标题】:Getting javaScript code from a script tag using Selenium: Is that possible?使用 Selenium 从脚本标签中获取 javaScript 代码:这可能吗?
【发布时间】:2023-04-05 12:42:01
【问题描述】:

下面代码的最后一行出现错误

// 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

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    是的,你可以通过使用像这样的属性来获取脚本的内容 el.getAttribute("innerHTML");

    【讨论】:

      【解决方案2】:

      据我所知,无法使用 XPath 访问“脚本”元素的内容。我发现这个问题的解决方案是在 HTML 源代码上应用正则表达式:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        • 1970-01-01
        • 2022-11-10
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多