【问题标题】:How can I scroll down a webpage using Selenium with VBA如何使用 Selenium 和 VBA 向下滚动网页
【发布时间】:2017-07-04 22:28:35
【问题描述】:

我使用 VBA 和 selenium 编写了一个脚本,以从一个网页中获取所有公司链接,该网页在滚动到最下方之前不会显示所有链接。但是,当我运行我的脚本时,我只得到 20 个链接,但总共有 1000 个链接。我听说在代码之间执行 javascript 函数可以完成这种类型的任务。在这一点上,我不知道如何将它放在我的脚本中。到目前为止,这是我尝试过的:

Sub Testing_scroll()

Dim driver As New WebDriver
Dim posts As Object, post As Object

driver.Start "chrome", "http://fortune.com/fortune500"
driver.get "/list/"

driver.execute_script ("window.scrollTo(0, document.body.scrollHeight);") --It doesn't support here

Set posts = driver.FindElementsByXPath("//li[contains(concat(' ', @class, ' '), ' small-12 ')]")

For Each post In posts
    i = i + 1
    Cells(i, 1) = post.FindElementByXPath(".//a").Attribute("href")
Next post

End Sub

【问题讨论】:

标签: vba selenium selenium-webdriver web-scraping


【解决方案1】:

根据 SeleniumBasic 中包含的示例,您应该使用

driver.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);")

不是“driver.execute_script”,它是来自the previous solution I gave you 的python 等效项:) 您将不得不以相同的方式循环它,直到您在页面上获得所有1000 个链接。

【讨论】:

  • 感谢乔治·麦康农的回答。它部分解决了我现在面临的问题。不过,我正在努力创建一个循环来完成这个过程。
  • 干得好! :) 您应该在此处添加您的解决方案,以便其他人可以从您的智慧中受益:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 2013-04-14
  • 2014-04-12
  • 1970-01-01
相关资源
最近更新 更多