Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 修改谷歌浏览器_Vba - Fatal编程技术网

Vba 修改谷歌浏览器

Vba 修改谷歌浏览器,vba,Vba,我有一个工作正常的代码。问题是它会打开Internet Explorer查看结果。我想在谷歌浏览器的结果。修改会有帮助 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

我有一个工作正常的代码。问题是它会打开Internet Explorer查看结果。我想在谷歌浏览器的结果。修改会有帮助

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
选项显式
子GetResults(r作为范围)
模糊的物体
作为对象的Dim frm
Dim srch作为对象
设置IE=CreateObject(“InternetExplorer.Application”)
可见=真实
即“导航”http://www.google.com"
在忙的时候做
Do While IE.ReadyState 4:DoEvents:Loop
设置frm=IE.Document.forms(“f”)
设置srch=frm.Document.all(“q”)
srch.Value=r.Value
提交
端接头

这将是为vba包装器编写的类似代码。安装selenium后,需要转到工具>引用>添加对selenium类型库的引用

这是按照你所写的字面意思写的。您可能希望考虑以ID而不是名称为目标,以及是否希望函数具体返回结果。

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
选项显式
公共子测试()
调光范围

Set r=[A1]“实际上,代码打开IE不仅仅是为了搜索结果,也是为了整个Google搜索请求自动化过程。Chrome不允许这样的自动化,你应该看看Selenium。在这种情况下,只需执行代码行:
CreateObject(“WScript.Shell”)。运行“chrome.exe”https://www.google.com/search?q=“&Cells(1,1)和”“”“,1,True
,注意可能需要为您的电脑指定
chrome.exe
的完整路径,以便代码看起来像
CreateObject(“WScript.Shell”)。运行”“C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”“”“https:…
。”。