fill out form using vba and chrome

2020-07-27 03:19发布

问题:

currently i can accomplish this using IE

Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Navigate "www.google.com"
ie.document.getElementByID("blah").value = "blah"

im curious if there is a way to navigate to website and fill out info using other than IE with VBA for example with FireFox or Chrome

i know how to navigate to other websites using any of the explorers for example Chrome as per below, but i would like to know how can fill out fields like search field on www.google.com using Chrome

call Shell("C:\Program Files\Google\Chrome\Application\chrome.exe"& " " & URL)

回答1:

You can use this Selenium wrapper to drive Chrome or Firefox (or PhantomJS). Note that it is currently in beta. Once it is installed, either add a reference to Selenium Type Library or use late binding (example below).

In my (very) limited tests, the only noticeable difference is the couple seconds it takes to load the driver.

Here is a simple example from the link above (modified to use late binding) that fills out the search field in Google and clicks the search button:

Dim selDriver As Object
Set selDriver = CreateObject("Selenium.WebDriver")

selDriver.Start "chrome", "http://www.google.com/"
selDriver.Open "http://www.google.com"
selDriver.Type "name=q", "Eiffel Tower"
selDriver.Click "name=btnG"

selDriver.stop


回答2:

As of 2016, the Selenium wrapper was moved to Github instead of the above Google address. Additionally, if you're using Chrome you'll want to update the web driver for Google Chrome here.

Selenium Basic

After downloading these items you'll then need to follow the instructions above for adding the library to in your Visual Basic Editor from within Excel. Afer doing so, you should be able fill out a web form using vba and chrome as originally requested.