修改为Google Chrome

时间:2018-09-08 16:50:58

标签: vba

我有一个工作正常的代码。问题是它会打开Internet Explorer以获得结果。我想要Google Chrome中的结果。修改会有所帮助。

Option Explicit

Sub GetResults(r As Range)
    Dim IE As Object
    Dim frm As Object
    Dim srch As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://www.google.com"
    Do While IE.Busy: DoEvents: Loop
    Do While IE.ReadyState <> 4: DoEvents: Loop
    Set frm = IE.Document.forms("f")
    Set srch = frm.Document.all("q")
    srch.Value = r.Value
    frm.submit
End Sub

1 个答案:

答案 0 :(得分:0)

这与为selenium basic vba包装程序编写的内容类似。安装硒后,您需要进入工具>参考>向硒类型库添加参考。

按照字面意思编写,以遵循您所写的内容。您是否还希望考虑使用id而不是name进行定位,是否想要一个函数专门从结果中返回某些内容。

Option Explicit
Public Sub test()
    Dim r As Range
    Set r = [A1]    '<== you really want a test that a single cell is passed or you will end up with an array later
    GetResults r 
End Sub

Public Sub GetResults(ByVal r As Range)
    Dim d As WebDriver, frm As Object, srch As Object
    Set d = New ChromeDriver
    Const URL = "http://www.google.com"

    With d
        .Start "Chrome"
        .get URL
        Set frm = .FindElementByName("f")
        Set srch = frm.FindElementByName("q")
        srch.SendKeys r.Value
        frm.submit
        Stop                                     '<=Delete me later
        .Quit
    End With
End Sub