使用 Excel VBA 控制 Web 浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7489418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:54:45  来源:igfitidea点击:

Controlling a web browser using Excel VBA

vbaexcel-vbaselenium-rcwebautomationexcel

提问by Darshan Bhatia

I have been assigned the task of automating a web based task ( for a HTTPS website). The users currently are filling in the Excel sheet with the data, they now want to automate excel in such a way that it directly controls the browser and fills in the data.

我被分配了自动化基于 Web 的任务(针对 HTTPS 网站)的任务。用户目前正在用数据填写 Excel 表格,他们现在希望以直接控制浏览器并填写数据的方式自动化 Excel。

I found the iMacros Scripting edition as a possible solution for doing this, I wanted to know if there are any other similar tools which can be used for controlling the browser and filling in data.

我发现 iMacros Scripting edition 是一个可能的解决方案,我想知道是否有其他类似的工具可用于控制浏览器和填充数据。

I also had a look at the Selenium Client Driver, but I am not sure on how to use it in Excel VBA.

我还查看了 Selenium Client Driver,但不确定如何在 Excel VBA 中使用它。

Any help would be appreciated.

任何帮助,将不胜感激。

Thanks,

谢谢,

回答by florentbr

You can use Selenium from Visual Basic Editor by installing the tools provided here :

您可以通过安装此处提供的工具从 Visual Basic 编辑器使用 Selenium:

http://code.google.com/p/selenium-vba/

http://code.google.com/p/selenium-vba/

There is a Selenium IDE plugin to automatically record a script in VBA and an installation package to run Selenium command in Visual Basic Editor.

有一个 Selenium IDE 插件可以在 VBA 中自动记录脚本,还有一个安装包可以在 Visual Basic 编辑器中运行 Selenium 命令。

The following example starts firefox, opens links in the 1st column, compares the title with the 2nd column and past the result in the 3rd column. Used data are in a sheet, in a range named "MyValues".

下面的示例启动 Firefox,打开第一列中的链接,将标题与第二列进行比较,然后将结果传递到第三列中。使用的数据位于工作表中,位于名为“MyValues”的范围内。

Public Sub TC002()
   Dim selenium As New SeleniumWrapper.WebDriver, r As Range
   selenium.Start "firefox", "http://www.google.com" 
   For Each r In Range("MyValues").Rows
     selenium.open r.Cells(, 1)
     selenium.waitForNotTitle ""
     r.Cells(, 3) = selenium.verifyTitle(r.Cells(, 2))
   Next
   selenium.stop
End Sub

回答by Bruno Leite

This sample open stackoverflow site an show IE

此示例打开 stackoverflow 站点显示 IE

Sub OpenIE()
'officevb.com
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")

ie.Navigate "http://www.stackowerflow.com"

 'wait load
 While ie.ReadyState <> READYSTATE_COMPLETE
  DoEvents
 Wend

ie.Visible = True

End Sub

[]'s

[] 的

回答by Sirga

I use this code for reading data from excel and passin it to selenium for to do task like "click, select, close etc" and also you can write data to excel.

我使用此代码从 excel 读取数据并将其传递给 selenium 以执行诸如“单击、选择、关闭等”之类的任务,您也可以将数据写入 excel。

This is in python i don know VB and i do know perl if u wish i'll give same code in perl too.

这是在 python 中,我不知道 VB,如果你希望我也会在 perl 中给出相同的代码,我也知道 perl。

i hop this may help.

我希望这可能会有所帮助。

from xlwt import Workbook

import xlrd

testconfigfilename="testconfig.xls"

    if (len(sys.argv) > 1):

        testconfigfilename=sys.argv[1]       

    wb = xlrd.open_workbook(testconfigfilename);

    wb.sheet_names();

    sh = wb.sheet_by_index(0); 'Sheet 0 - selenium server configuration'



    seleniumHost = sh.cell(1,0).value

    seleniumPort = int(sh.cell(1,1).value)

    testBaseURL = sh.cell(1,2).value

    browser = sh.cell(1,3).value

    timeout = int(sh.cell(1,4).value)

    path = sh.cell(1,5).value

outputwb = Workbook()

    outputsheet = outputwb.add_sheet("result",cell_overwrite_ok=True) #get the first sheet in the result xls 

outputsheet.write(RowNumber,colNumber,"data")