Interact with Google Chrome on VBA - excel

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

Related

VBA Selenium with Chrome - Upload a file to the WebPage

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"

Selenium Chromedriver Won't Keep Me Logged In When Interacting With Website... Sometimes

I'm creating a program through Microsoft Excel VBA using Selenium Chromedriver. The program aims to take information from a Spreadsheet, create links, then open the pages and fill form fields with information from the spreadsheet. I've been able to get most all of the functionality to work, with one annoying hiccup. Each time the program opens a new link, the website signs me out of my profile and returns it to the default Guest profile.
Sub startingout()
Dim URL As String
Dim driver As New WebDriver
driver.Start "Chrome", ""
driver.Get "https://website.com/default.aspx"
driver.FindElementByName("ctl00$LoginForm$UserName").SendKeys ("username")
driver.FindElementByName("ctl00$LoginForm$Password").SendKeys ("password")
driver.FindElementByName("ctl00$LoginForm$LoginButton").Click
URL = "https://website.com/specificURL"
Call secondtest(driver, URL)
End Sub
Sub secondtest(driver As WebDriver, URL As String)
driver.Get URL
driver.FindElementByName("ctl00$ContentPlaceHolder1$lvItems$ctrl0$TextBoxQty").SendKeys ("1")
driver.FindElementByName("ctl00$ContentPlaceHolder1$lvItems$ctrl0$btnAddCart").Click
Application.Wait (Now + TimeValue("0:00:01"))
driver.FindElementByName("ctl00$ContentPlaceHolder1$btnContinue").Click
Application.Wait (Now + TimeValue("0:00:1"))
End Sub
Above is the test code that works fine. I stay logged in. The actual program is a sub procedure that calls one of three other sub procedures when the link is created. Below is how I began the main sub procedure, to load up the website.
Dim driver As New WebDriver
driver.Start "Chrome", ""
driver.Get "https://website.com/default.aspx"
driver.FindElementByName("ctl00$LoginForm$UserName").SendKeys ("username")
driver.FindElementByName("ctl00$LoginForm$Password").SendKeys ("password")
driver.FindElementByName("ctl00$LoginForm$LoginButton").Click
Once the link is created, it moves to another sub procedure that starts as follows:
Sub FullBar(URL As String, Quantity As Integer, CurRow As Integer, driver As
WebDriver)
Dim gotit As Boolean
gotit = False
driver.Get URL
Application.Wait (Now + TimeValue("0:00:01"))
Now, it does not seem that these two cases are fundamentally different, yet they act like they are. The strange thing about this is that when I create a test program using just VBA and some test links, I am able to stay logged in just fine. Why is this, and what makes the two situations different?
I didn't really figure out why this is happening, but I did find a solution. I followed this page:
https://www.reddit.com/r/vba/comments/a63c2k/open_chrome_and_set_profile_with_chromedriver/
I created a new chrome profile and loaded from there, adding the
bot.addArguments("--no-sandbox")
to the code. It could be that the Selenium doesn't like using the default program or something. Anyway, I got it to work and I stay logged in.

VBA - chrome not working in the background

I Have a code in VBA with Selenium that does a search on the website and then clicks Download button. The code works fine if I am not making it work in the background. Once I set Chrome to work in the background (adding .AddArgument ("headless")) , the code does not download anything. The code is nothing complicated, I just want it to perform the download in the background.
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "Here I put the website"
With d
.AddArgument ("headless")
.Start "Chrome"
.get URL
.wait 1000
.FindElementById("Search").SendKeys " Where I insert the search Information "
.wait 2000
.FindElementById("search").Click
.wait 2000
.FindElementById("download").Click
.wait 2000
End With
Code works fine without the .AddArgument ("headless"). If I use it, it does not give me an error but I do not download the CSV.
You need to set window-size() while setting chrome browser in headless mode.
With d
.AddArgument ("headless")
.AddArgument "window-size=1920,1080"
.Start "Chrome"
End With
Let me know if this work for you.
You can try setting the attribute instead such that:
.FindElementById("Search").SendKeys
becomes
.ExecuteScript "document.querySelector('#Search').setAttribute('value','Where I insert the search Information');"
You may need to alter 'value' to another attribute e.g. 'textContent'.

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

Resources