【问题标题】:Store Selenium HTML Source Code into element of type HTMLDocument将 Selenium HTML 源代码存储到 HTMLDocument 类型的元素中
【发布时间】:2018-01-28 20:43:55
【问题描述】:

是否可以将使用 Selenium(使用 Excel VBA)抓取的 HTML 源存储到 HTMLDocument 元素中? 这是一个使用 Microsoft Internet ControlsMicrosoft HTML Object Library 自动化 Internet Explorer 的示例。

Dim IE as InternetExplorer
Dim HTML as HTMLDocument
Set IE = New InternetExplorer
ie.navigate "www.google.com"
set HTML = IE.Document

Selenium 的圆顶也可以吗?例如(不工作!):

Dim selenium As SeleniumWrapper.WebDriver
Set selenium = New SeleniumWrapper.WebDriver
Dim html as HTMLDocument

selenium.Start "firefox", "about:blank"
selenium.Open "file:///D:/webpages/LE_1001.htm"
Set html = selenium.getHtmlSource 'this is not working since .getHtmlSource() 
                                 'returns a String object but is there a way to store 
                                 'this html source into a type of HTMLDocument-element

【问题讨论】:

    标签: html vba excel selenium xhtml


    【解决方案1】:

    使用 SeleniumBasic 获取 DOM 的正确方法:

    Sub Get_DOM()
      Dim driver As New FirefoxDriver
      driver.Get "https://en.wikipedia.org/wiki/Main_Page"
    
      Dim html As New HTMLDocument  ' Requires Microsoft HTML Library
      html.body.innerHTML = driver.ExecuteScript("return document.body.innerHTML;")
    
      Debug.Print html.body.innerText
    
      driver.Quit
    End Sub
    

    要使用上述示例获取最新版本: https://github.com/florentbr/SeleniumBasic/releases/latest

    【讨论】:

    • 这是用于获取 html 正文。如何将 html 正文加载到网页我的意思是将页面加载到基于 html 正文的 selenium 驱动程序中?
    【解决方案2】:

    这应该可以使用字符串作为 HTML 文档的源:

    Set html = New HTMLDocument
    html.body.innerHTML = selenium.pageSource
    

    编辑:将 Selenium 调用从 getHtmlSource 更改为 pageSource。完整的工作代码如下。但不确定我们使用的是相同版本的 Selenium:

    Option Explicit
    
    Sub foo()
    
    Dim sel As selenium.WebDriver
    Set sel = New selenium.WebDriver
    Dim html As HTMLDocument
    
    sel.Start "firefox", "about:blank"
    sel.Get "http://www.google.com/"
    
    Set html = New HTMLDocument
    html.body.innerHTML = sel.PageSource
    
    Debug.Print html.body.innerText
    
    End Sub
    

    引用 Microsoft HTML 对象库和 Selenium 类型库 (Selenium32.tlb) - 使用 SeleniumBasic 版本 2.0.6.0

    【讨论】:

    • 在线html.body.innerHTML = selenium.getHtmlSource 我收到Run-time error: 438: Object doesn't support this property or method ...对此有什么想法吗?
    • 我已经更新了我的答案 - 我认为错误来自对 Selenium 的调用,而不是来自 HTMLDocument
    • 非常感谢您的回复。它有一个小问题,即原始(“google.com”)中的HTML code 具有比插入HTMLDocument 对象的源代码更多的字符:原始具有425.640 和HTMLDocument 对象具有@987654328 @... 所以并非所有代码都被复制。你对这个有什么想法吗?谢谢。
    • 你在比较同类吗?我在示例中使用 body.innerText 只是为了将打印的文本量保持在可管理的范围内。要与原始的长度进行比较,请查看 html.body.innerHTML 的长度。由于消除了不必要的空白,innerHTML 可能比原始文档短一些
    【解决方案3】:

    不太清楚为什么您更喜欢将 Selenium 元素转换为 HTMLDocument。它需要对你的项目多一个有界的依赖。

    我个人更喜欢将 DOM 元素分配给 WebElement。例如:

    If (Selenium.FindElementsByClass("qty").Count > 0) Then
        Dim qtyElement as WebElement: Set qtyElement = Selenium.FindElementByClass("qty")
    End If
    
    If (Not qtyElement is Nothing) then
        Dim qtyHtml as String: qtyHtml = qrtElement.Attribute("innerHTML")
    End if
    
    Debug.Print qtyHtml 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2012-04-15
      • 2018-04-08
      相关资源
      最近更新 更多