2017-07-04 55 views
-1

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

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 
+0

我认为如果您的Excel版本支持它,Power Query可以自动执行此操作,或者对于旧版本,动态Web查询可以自动执行https://www.vertex42.com/News/excel-web-query.html – Slai

回答

0

根据附带SeleniumBasic的例子,你应该使用

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

不是“driver.execute_script”,这是蟒蛇相当于从the previous solution I gave you的: )你将不得不以相同的方式循环,直到页面上有所有1000个链接。

+0

感谢George McConnon,为你的答案。它部分解决了我现在面临的问题。不过,我正在努力创建一个循环来完成这个过程。 – SIM

+0

我已经知道了。 – SIM

+0

干得好! :)你应该在这里添加你的解决方案,以便其他人可以从你的智慧中受益:D – NotInventedHere

相关问题