Excel VBA - auto-populate a form - excel

I have a small Macro taking me a to a website on Chrome using a shell command, but I'm hoping to chain it to another that populates the username and password on the site. Is this possible? My code so far:
Sub Logintest2()
Dim chromePath As String
chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url http://www.google.com")
End Sub

I found a post on Reddit that says this is much easier using Internet Explorer since Excel/VBA has some built in interop. However, if you're anything like me you abhor IE.
The same Reddit post suggested a third party plug-in called Selenium Basic. I used this answer to download and install it.
The following code successfully opened Google in Chrome and set the text of the search box to "This is a test".
Sub Test()
Dim bot As New WebDriver
bot.Start "chrome", "https://www.google.com"
bot.Get "/"
bot.FindElementByName("q").SendKeys "This is a test"
Stop
End Sub
I used DevTools (F12) to explore the web content and find that the search box has name="q".
Good luck!

Related

Send Keys from VBA to chrome by Chrome DevTools Protocol

I want to automate Chrome from Excel VBA. I am using the framework mentioned in : (Method 2 in the answer)
Automating Edge Browser using VBA without downloading Selenium
where in github located
https://github.com/longvh211/Chromium-Automation-with-CDP-for-VBA
In the browser I want to automate, there is a search input and looking at the sample I can select the input like this
chrome.jsEval "el = document.evaluate(""//input[contains(#placeholder,'Search')]"", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"
And I can change its value like :
chrome.jsEval "el.value = """ & WorkSheet.Cells(2, 3) & """"
However the input has autocomplete (typeahead) and when I change its value like above, it doesn't filter the table below it. I believe I should send the value by sendkey like in Selenium, but couldn't figure out how to do this with this framework.
I would appreciate any help on this issue.

Is there any way to get specific url after hitting a URL from VBA

basically I need to call a URL(some auth based) from my VBA script. when i call it, it will open in browser with respective screen, after authenticating that page it will directing to other url in same page. i want to load that URL in my VBA code.
Can you please help me. which library i need to use in VBA. i tried normal hitting with shell() Shell(chrome & " -url " & url) but by this there is no control on browser to VBA. also i tried selenium. i am unable to locate the URL.
Dim driver As WebDriver
Set driver = New WebDriver
driver.Start "chrome"
driver.Get url
driver.Wait 10000
Url = driver.Url
Application.Wait Now + TimeValue("00:00:20")
Please suggest required

Using Selenium ChromeDriver with VBA Error

I'm a beginner with both VBA and Selenium so forgive me if this is too obvious. I'm trying to see whether I can make VBA open a Chrome window to check that Selenium works fine, via the code below:
Option Explicit
Private MyBrowser As Selenium.ChromeDriver
Sub TestSelenium()
Set MyBrowser = New Selenium.ChromeDriver
MyBrowser.Start
End Sub
I, however, get the following error:
Does anyone know what I could be doing wrong?
Thanks so much
As it turns out I had to download Microsoft .NET 3.5 Framework for it to work

Use VBA to open URL in Default-Browser an catch existing Session

I try to open a specific URL of a web-application I'm already logged in (or tells me to login if I'm not) in the default browser (Chrome). When I copy/paste this URL into the browser address bar, it perfectly works. It doesn't when I open this URL by VBA with ThisWorkbook.FollowHyperlink - then it redirects - as a kind of fallback - to the homepage instead the specific URL.
I found out that this is a session problem and VBA somehow doesn't recognize/catch the existing session.
As "ugly workaround" I'm currently redirecting over http://www.dereferer.org/ to the specific URL, what perfectly works, but is needing additional time.
This doesn't work:
ThisWorkbook.FollowHyperlink ("https://www.example.com/function/edit/2019-04-09)
This works:
ThisWorkbook.FollowHyperlink ("http://www.dereferer.org/?https://www.example.com/function/edit/2019-04-09)
(for my needs it's not required to encode the target URL)
As this redirect is slow and indirect, I'm searching for a way to directly open the targeted URL while using the existing session (if possible). If this isn't possible (for example because of security), what's the best/fastest way to redirect without setting up an own redirector (which redirects like dereferer.org over a GET parameter)?
A clunky and ill-advised workaround, but you could bypass FollowHyperlink, and instead use Shell to open the website in a new tab/window of your default web-browser:
Shell "explorer ""https://www.example.com/function/edit/2019-04-09"""
(As a note, if you type as a hyperlink in a cell and clicked on it manually, instead of using VBA FollowHyperlink, then the same issue would still occur. This also happens in Word and PowerPoint. Just be thankful you're not trying to catch the FollowHyperlink event and "correct" that in the window)
In response to comments - for Mac you will need to use "open" instead of "explorer". This code should run on both Mac or PC:
Shell IIf(Left(Application.Operatingsystem, 3)="Win","explorer ","open ") & _
"""https://www.example.com/function/edit/2019-04-09"""
If you are allowed to install selenium basic I would use that
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 = "url"
With d
.Start "Chrome"
.get URL
'login steps
.get 'otherUrl'
Stop '<delete me later
.Quit
End With
End Sub

Access web page body text using VBA & Selenium

I am trying to convert an Excel macro that currently uses Internet Explorer and use the following line of code to extract the web page’s <body> text
x = .Document.DocumentElement.InnerText
Using the Selenium demo, I am able to produce a jpg of the page with Chrome & IE, but Firefox just loads a blank page and IE64 & Edge don’t work on Windows 10.
I have been unable to find the proper VBA command with Selenium to copy the body text to variable ”x”. I only want to read it.
I am trying to do this to make my macro browser independent.
The macro is for my use only.
Jim
You are not making it browser agnostic. You are simply widening the choice of browser to those supported via selenium basic. This brings some problems of its own which you are noticing.
Folders containing the drivers must be on the environmental path or the path passed to selenium webdriver as an argument.
You should use the latest Chrome browser and Chrome driver
You cannot use the latest FireFox browser and driver. It is not supported. I think you need FF v.46.0.1.
If using IE then zoom must be to 100%.
I suggest browsing the issues pages of Github for further known issues
Heuristically, I have heard some banter about problems with Windows 10 and Selenium Basic - would be interested to know if anyone has got this working as I am not on that version.
Review the examples.xlsm provided by selenium basic GitHub site to see which other browsers are supported (e.g. Opera, PhantomJS, FirefoxLight,CEF).
With Chrome you can get the body text with this:
Option Explicit
Public Sub GetInfo()
Dim d As WebDriver, s As String
Set d = New ChromeDriver
Const URL = "https://www.neutrinoapi.com/api/api-examples/python/"
With d
.Start "Chrome"
.get URL
s = .FindElementByTag("body").Text
Debug.Print s
.Quit
End With
End Sub
Other info: https://stackoverflow.com/a/52294259/6241235

Resources