How to automate Edge browser using Excel macros without Selenium?
We have a requirement where we need to automate Edge browser using VBA/Excel macros. I found several websites which explains how to achieve it using Selenium but none of them explain the process in detail. Can anyone help me with this? If you have a valid account and are allowed to use the cloud or some testing environment, then it's very easy to do this. Here is a working example, using the internet explorer automation library. It will download the edge browser and run the scripts, and exit when done.
Option Explicit. Const IEVERSION = 12. ' Declare variables. Dim oEdge As Object, bEdgeDownloaded As Boolean. Dim sUrl As String, sScript As String, sUserAgent As String. ' The script that downloads the edge browser. SUrl = "". SScript = """". SUserAgent = """" & Browser("MicrosoftEdge"). ' The script that runs the automation. Var oShell = new ActiveXObject("Shell.Application"); var oInternetExplorer = oShell.HtmlDocument.parentWindow;
OInternetExplorer.Navigate2(sUrl); while (oInternetExplorer.Busy) ' Download the edge browser and run the scripts. ' Note: this assumes you are on a testing environment. BEdgeDownloaded = True. ' Start the edge browser download. Set oEdge = CreateObject("MSXML2.XMLHTTP") oEdge.Open "GET", sUrl, False oEdge.Send() Do While bEdgeDownloaded. DoEvents. Loop. ' Run the automation script. With CreateObject("WScript.Shell") .
Can you web scrape with VBA?
Can I use VBA to pull data out of different sites? And then make a new workbook with that data, for each site? So for each link I'm able to get a webpage of data, and then that data is saved in a new sheet. My question is if I can "paste" this data into an existing workbook? If not, how I should create a new one? Should I just create a new workbook for each data scrape or is it best to take advantage of the workbook for each link/website combination? I'm asking myself: is this even a good way to save all this scraped data? I was able to figure it out myself. Here's what I did: Sub ScrapeData(). Dim obj As Object. Dim Wsht As Worksheet. Dim myurl As String. Dim wb As Workbook. With ThisWorkbook.Sheets("TESTING - New Sheets") myurl = .Range("a1").Value
End With. Set Wsht = ThisWorkbook.Worksheets.Add
Wsht.Name = "MyScrapedData" Set obj = CreateObject("MSXML2.XMLHTTP") obj.Open "GET", myurl, False obj.send Do While obj.readyState <> 4 Application.Wait DateAdd("s", 1, Now) Loop. If obj.Status = 200 Then Set Wsht = ThisWorkbook.Sheets(1) obj.GetResponseText = Wsht.Cells(1, 1).Value
MsgBox obj.Status MsgBox obj.GetResponseText Else. MsgBox "Status=" & obj.Status & ", ResponseText=" & obj.GetResponseText
Related Answers
How can we use the Selenium tool with HeadSpin?
Selenium is a tool that is used to automate functional testing. There are two types...
What are 5 Uses of Selenium?
Selenium is a web-automation tool that helps you to test web applications....
How can we use the Selenium tool with HeadSpin?
Selenium is a cross-browser testing automation framework w...