为Google Chrome创建一个对象

时间:2016-02-12 11:40:13

标签: html vba internet-explorer web-scraping

我想将以下代码用于Google Chrome而不是IE。 Public Sub GetValueFromBrowser()     昏暗即作为对象     Dim url As String     Dim myPoints As String

url = "http://localhost:8080/xyz"
Set ie = CreateObject("InternetExplorer.Application")

With ie
  .Visible = 0
  .navigate url
   While .Busy Or .readyState <> 4
     DoEvents
   Wend
End With

Dim Doc As HTMLDocument
Set Doc = ie.document

myPoints = Trim(Doc.getElementsByName("points")(0).Value)
Range("A1").Value = myPoints

End Sub

3 个答案:

答案 0 :(得分:1)

改为使用selenium basic VBA包装器(假设其余代码准确无误)

Option Explicit

Public Sub GetValueFromBrowser()
    'tools > references > selenium type library
    Dim d As chromedriver, myPoints As String

    Const URL As String = "http://localhost:8080/xyz"
    Set d = New chromedriver

    With d
        .Start
        .Get URL  
        myPoints = Trim$(.FindElementByName("points")(0).Value)
        ActiveSheet.Range("A1").Value = myPoints
        .Quit
    End With
End Sub

安装selenium basic后,您需要添加selenium参考。去VBE&gt;工具&gt;参考文献&gt; Selenium类型库

答案 1 :(得分:0)

试试这个

   var test =               [{"code":"Rooms","value":"1"},ode":"Rooms","value":"7"},{"code":"Rooms","value":"6"},{"code":"Rooms","value":"4"},{"code":"Rooms","value":"3"},{"code":"Rooms","value":"2"},{"code":"Rooms","value":"16"},{"code":"Rooms","value":"15"},{"code":"Rooms","value":"14"},{"code":"Rooms","value":"13"},{"code":"Rooms","value":"12"},{"code":"Rooms","value":"11"},{"code":"Rooms","value":"9"}];`
   for(var i = 0, len = test.length; i < len; i++) {
        roomList[i] = test[i].value;    //ERROR        
   }; 

答案 2 :(得分:0)

最好的解决方法可能是在本文中回答使用Shell:

Using Chrome browser instead of InternetExplorer.Application

基本上分2步:

  1. 找到计算机中Chrome.exe的路径。
  2. 获取您的网址。
  3. 在Chrome上运行Google搜索页面的示例:

    Dim chromePath As String
    chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
    Shell (chromePath & " -url http:google.ca")