【问题标题】:Get iframe content by using Javascript XPATH in Javascript executor for selenium webdriver在用于 selenium webdriver 的 Javascript 执行器中使用 Javascript XPATH 获取 iframe 内容
【发布时间】:2014-06-30 08:05:34
【问题描述】:

我正在尝试使用以下代码移动到框架,然后获取内容,但它返回的是空内容文档。

var iframe = document.evaluate('//iframe[contains(@src, \"//my.leadpages.net/forms/5620062778032128/5654100301578240/5645914630782976/html/?lp-in-iframe=1\")]',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).contentDocument;alert(iframe);

另外,如果你能指导我如何移动到框架,然后通过节点获取它的内容。Iframe 没有 ID、名称等,所以我使用了 xpath 方式。它正在检测框架作为结果 XpathResult 但我是不知道如何进入它,然后为各种节点提取文本。这必须在 Javascript 中。

【问题讨论】:

    标签: javascript xpath selenium-webdriver web-scraping


    【解决方案1】:

    如果您提供了一些带有 iframe 的 html,但请尝试以下代码,这会更容易为您提供帮助。它只能用 JAVA 来实现,你不需要 javascript。

    driver.switchTo().frame(driver.findElement(By.xpath("frame xpath expression here")));
    //do your stuff here.
    //to get the content of this frame, it depends on the structure of the html
    //if content is within some div then simply do this.
    //this would be the css selector for some DIV where the content is inside the iframe:     
    //div#divContentID
    WebElement divContent=driver.findElement(By.cssSelector("div#divContentID"));
    String content=divContent.getText();
    //to switch out of the iframe back to the main content.
    driver.switchTo().defaultContent();
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 2016-12-04
      • 2015-09-04
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2019-06-14
      相关资源
      最近更新 更多