VBA Selenium with Chrome - Upload a file to the WebPage - excel

I am using VBA to automate Chrome to upload a file to a site, with the button of File type
I have no idea about uploading script in chrome
My current VBA:
Sub UploadFile()
Dim bot As WebDriver
Set bot = New WebDriver
bot.Start "chrome"
bot.Get "https://tutorialehtml.com/en/html-tutorial-upload-form/"
'#continue script here please"
End Sub

If there is any input field with attribute type = 'file' you can use the below code :
bot.FindElementByCss("input[type='file']").SendKeys "path of file"

Related

"User-defined type not defined" error for a simple code in excel-VBA

I wrote a simple code to open my browser from Excel-VBA with help of Selenium below; However, I got an error that says: "Compile Error: User-defined type not defined".
I tried to go to the references and add WebDriver to my Excel, but "References" option is not available under Tools category in my Excel.
Public Sub seleniumtutorial()
Dim bot As New WebDriver
bot.Start "internet explorer", "http://google.com"
bot.Get "/"
bot.TakeScreenshot.SaveAs (ActiveWorkbook.Path + "/screenshot.jpg")
bot.Quit
'quit method for closing browser instance.
End Sub
Whenever you couldn't see References option available try to run your Excel as an administer.

Receiving KeyNotFoundError Dictionary key not found: status when using the .click method

I'm currently trying to learn VBA in Excel and I've written a simple procedure where I should be able to launch Chrome and click on a button. Problem is that I'm getting a Run-time error '0': KeyNotFoundError Dictionary key not found:status whenever I try to use the .click method.
I downloaded the Selenium Basic from the following site https://florentbr.github.io/SeleniumBasic/ activated the reference in VBA and downloaded the latest chromedriver
Sub driver()
Dim driver As New Selenium.WebDriver
Set driver = New Selenium.WebDriver
driver.Start "chrome"
driver.Get "http://www.google.com"
Set Element = driver.FindElementByName("btnI")
Element.Click
End Sub
This is just a simple code where I launch Chrome and go to Google and click the "I feel Lucky" button.
A quick test for firing that button and I needed to use javascript
driver.ExecuteScript "document.querySelector('[name=btnI]').click();"
If the error remains I suggest checking chromedriver.exe is the right version for your Chrome browser and do a selenium re-install and check the selenium type reference as usual in the VBE.
The latest version of Chrome is throwing "KeyNotFoundError Dictionary key not found:status" error.
Try replacing your Chromedriver with version 2.46. Here is the link: https://chromedriver.storage.googleapis.com/index.html?path=2.46/
Below is the code I have tried and it worked perfectly fine for me. Let us know if this solution worked for you! :)
Sub driver()
Dim driver As New Selenium.WebDriver
Set driver = New Selenium.WebDriver
driver.Start "chrome"
driver.Get "http://www.google.com"
driver.FindElementByCss("#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input[type=submit]:nth-child(2)").Click
End Sub

Avoid closing the browser

I wrote a macro on VBA using Selenium.ChromeDriver, which opens the site and gives it various actions.
But the problem is that if an error occurs in the macro and I click "Stop" in debugging mode, then the browser closes.
The problem is that when you close the browser and open it, you have to enter your login and password each time.
How can I avoid closing the browser opened via Chrome Driver if the macro ends or when an error occurs?
Did so
Public Driver as new ChromeDriver
but that doesn't help, the browser closes.
Need some more code really but some pointers
When error occurs click debug rather than end and the browser should remain open
Don't use .Quit if you want automated browser to remain open at end of sub. However, you will need to grab the session id if you wish to reconnect with that instance, or pass the driver object to any receiving sub. Just remember at some point to Quit.
Here is how you open another tab:
Option Explicit
'download selenium https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
'Ensure latest applicable driver e.g. ChromeDriver.exe in Selenium folder
'VBE > Tools > References > Add reference to selenium type library
Public Sub DownloadFile()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "www.something/login/"
With d
.Start "Chrome"
.get URL
.FindElementById("name").SendKeys ""
.FindElementById("email").SendKeys ""
.FindElementByCss("[type=submit]").Click
Application.Wait Now + TimeSerial(0, 0, 5) '< better to have a wait condition for something on the post login page
.ExecuteScript "window.open(" & Chr$(34) & "postloginUrl" & Chr$(34) & ",'_blank');"
Stop
.Quit
End With
End Sub

Can't use Selenium with the Company Modified Browser

The Company change the browser name to XYZFirefox.exe . And since then, my code is getting an error. "Failed to locate Firefox.exe in the registry". I'm trying it again with the simple code. Does renaming the exe affects it? I would like to sort this first before mailing our IT Dept. Thanks
Dim bot As New FirefoxDriver
Dim url As String
url = "https://google.com"
bot.Get url
You can specify a path to binary:
Dim bot As New FirefoxDriver
bot.SetBinary("path to your custom Firefox")

Interact with Google Chrome on VBA

From an excel file in VBA, I would like to click on the big button on this webpage (https://www.ilovepdf.com/jpg_to_pdf), select a file from my computer to download to this website (select the right path), validate, download the new file, then close the tab.
I have for the moment manage to open the browser but I can interact with the website..
Thanks for your help :)
Sub ConvertFile()
ChromeLocation = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" 'Location of Chrome.exe in your PC
MyURL = "https://www.ilovepdf.com/jpg_to_pdf" 'URL that you would like to open; you may also use a variable for this, or a pointer to the location of the URL
Shell (ChromeLocation & " -url " & MyURL)
End Sub
You could use selenium basic to launch and interact with chrome. I am showing how to launch, enter a search term, and click a button. The rest of your question is simply too broad. The example will bring up the download site in the search results. After installation you will need to add a reference to selenium type library.
Option Explicit
Public Sub Demo()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://www.google.com/"
With d
.Start "Chrome"
.get URL
.FindElementById("lst-ib").SendKeys "Selenium basic GitHub"
.FindElementsByTag("form")(1).FindElementByCss("input[value='Google Search']").Click
'.Quit
End With
End Sub

Resources