【问题标题】:Selenium Wrapper not getting the correct html source pageSelenium Wrapper 没有得到正确的 html 源页面
【发布时间】:2016-01-22 15:40:28
【问题描述】:

我正在编写一个脚本来自动执行一些操作。我正在使用 Selenium Wrapper (chrome) 打开一个页面,然后等待 5 秒(该页面有一些 javascript/ajax,会更改页面),然后我要求提供源代码。我得到了初始 html,而不是 js 和 ajax 更改(如果我手动转到导航器并右键单击 -> 源代码,我会得到正确的源代码)。

我的 VBA 代码是:

Dim selenium As New SeleniumWrapper.WebDriver

selenium.Start "chrome", "http://about:blank"
selenium.setTimeout ("120000")
selenium.setImplicitWait (5000)
intRowPosition = 2
selenium.Open "http://www.somepage.com/%3Fstart%3D1"
selenium.sleep ("5000")

msgbox(selenium.getHtmlSource)

知道怎么解决吗?

【问题讨论】:

    标签: ajax vba excel selenium selenium-webdriver


    【解决方案1】:

    getPageSource 的文档,明确指出

    如果页面在加载后已被修改(例如,通过 Javascript),则无法保证返回的文本与修改后的页面相同。

    但这也可能是由于同步问题。我对VBA框架不熟悉,所以不知道它是否有explicit wait的等价物,但如果有,最好等待页面完全加载时才会出现的特定情况(“用js和 ajax 更改”,而不是“睡眠”,它不能保证页面在等待结束时准备好。

    【讨论】:

    • 我不认为是等待问题,因为我在调试时遇到了同样的问题并等待很长时间直到执行最后一条指令
    • @MarcoMartin 很容易实现您在“官方”驱动程序之一(即 csharp 驱动程序)上共享的代码。因此,如果页面上的问题无法与其他驱动程序复制,则可能是驱动程序实现有问题,在这种情况下,您必须将其报告给该驱动程序的开发人员或切换到另一个驱动程序
    【解决方案2】:

    SeleniumWrapper 已弃用且不再受支持。 它已被 SeleniumBasic 取代: https://github.com/florentbr/SeleniumBasic/releases/latest

    获取页面来源:

    Dim driver As New ChromeDriver
    driver.Get "https://en.wikipedia.org"
    
    MsgBox driver.PageSource
    MsgBox driver.ExecuteScript("return document.documentElement.innerHTML")
    MsgBox driver.ExecuteScript("return document.body.innerHTML")
    

    【讨论】:

      猜你喜欢
      • 2018-01-20
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多