Navigating a Web Browser Using Excel VBA and Selenium Basic (Automation)
The general format consists of an object declaration and then a call to a function.
Object declaration:
Dim selenium_obj As New Selenium.[Chosen Web Driver]
There are several possible web drivers
- ChromeDriver: For the Google Chrome Web Browser
- EdgeDriver: For the Microsoft Edge Web Browser
- FirefoxDriver: For the Mozilla Firefox Browser
- IEDriver: For the Microsoft Internet Explorer Web Browser
- OperaDriver: For the Opera Web Browser
- PhantomJSDriver: For the PhantomJS Browser
- WebDriver: General use
*There is also the ability to read PDF files using PdfFile
Web navigation – open a browser and navigate to a page:
selenium_obj.Start(Browser_Name, URL)
Parameter | Description |
Browser_Name | REQUIRED. The name of a web browser in string form.
|
URL | REQUIRED. The navigation location, a URL or file location. |
Web navigation – when a browser is already open:
selenium_obj.Get(URL, Timeout, Raise)
Timeout and Raise are optional parameters.
Parameter | Description |
URL | REQUIRED. The navigation location, a URL or file location. |
Timeout | OPTIONAL. The amount of time the application should try to navigate the web browser to the specified URL |
Raise | OPTIONAL. Boolean value specifying whether an alert should be issued when Timeout duration has expired. |
Web navigation – return to the previous page:
selenium_Obj.GoBack
Goes back one page, like pressing a web browser’s back button.
Web navigation – move forward one page:
selenium_Obj.GoForward
Navigates the browser forward one page, like pressing the web browser’s forward button.
Example 1: Start()
Private Sub CommandButton1_Click()
Dim selenium_Obj As New Selenium.ChromeDriver
Call selenium_Obj.Start("chrome", "https://agile-mercurial.com")
selenium_Obj.Wait 5000End Sub
The above code should open up the Chrome web browser and navigate to https://agile-mercurial.com. It should also wait for 5 seconds.
Example 2: Get()
Private Sub CommandButton1_Click()
Dim selenium_Obj As New Selenium.ChromeDriver
Call selenium_Obj.Start("chrome", "https://agile-mercurial.com")
Call selenium_Obj.Get("https://google.com")
selenium_Obj.Wait 5000
End Sub
The code above will open a browser, navigate to https://agile-mercurial.com, then navigate to https://google.com. It should also wait for 5 seconds.
Example 3: GoBack()
Private Sub CommandButton1_Click()
Dim selenium_Obj As New Selenium.ChromeDriver
Call selenium_Obj.Start("chrome", "https://agile-mercurial.com")
Call selenium_Obj.Get("https://google.com")
Call selenium_Obj.GoBack
selenium_Obj.Wait 5000
End Sub
This code will go to one web page, then a second page, and then it will return to the first page.
Example 4: GoForward()
Private Sub CommandButton1_Click()
Dim selenium_Obj As New Selenium.ChromeDriver
Call selenium_Obj.Start("chrome", "https://agile-mercurial.com")
Call selenium_Obj.Get("https://google.com")
Call selenium_Obj.GoBack
Call selenium_Obj.GoForward
selenium_Obj.Wait 5000
End Sub
The above code navigates to one page, then a second page. It will then go back to the first page before finally going forward to the second page.
0 comentarios:
Publicar un comentario