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

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

Related

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

Can you make a node server code communicate with HTML of a website open in browser?

For example I've written a code to access a global variable of a web page and access values from it and then I want to put that value in some HTML div of that page. Now I can copy and paste this code into browser console and it will work.
But instead of pasting it in console, is there any way I can run my own server (localhost) using NodeJS and do the same from there? I mean communicate to the browser and that page which is open?
If yes, what things will I need?
I would use selenium-webdriver
It takes a bit of setup, but afterwards it's pretty easy
go here and click on the folder just above the one that says 'icons' (this should be the latest version of chrome webdriver)
Then, once you download the latest version, drag the exe C:\WINDOWS (if you're not on windows just move it to any folder on the PATH environment variable
now that you've done that, set up a simple script like this:
const webdriver = require('selenium-webdriver')
let driver = new webdriver.Builder().forBrowser('chrome').build();
(async function example() {
await driver.get('http://example.com/')
await driver.executeScript('/* your code, for example: */ document.getElementsByTagName(\'h1\')[0].innerHTML = \'test\'; console.log(\'test\')')
}())
make sure to escape (but a backslash before) any single quotes in your driver.executeScript
I would recommend learning a bit about selenium-webdrivers api so that you can to more complicated things (such as running this without actually opening a new window). I would also that you use selenium-webdriver's api (which allows you to edit webpages, click things, input keys, etc.) instead of just putting everything in an executeScript as much as you can.

Excel VBA - auto-populate a form

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!

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

get current Url in codedUI

I am using CodedUI to automate my site .How will i get the current url .
i am launching http://example.com ,but later in my application i am appending E=U and E=P to it bases on some conditions .I want to know how to identify if E=U /E=p ?
I think this should work:
BrowserWindow browser = BrowserWindow.Launch();
//Some Code Here
Uri uri = browser.Uri;
string st = uri.ToString();
BrowserWindow browser = BrowserWindow.Launch();
//Some Code Here
string st = browser.Uri.ToString();
The address bar of the browser contains the URL and its contents can be accessed in at least two ways:
By accessing the value of the field in the same fashion as would be done by a Coded UI assertion generated by the recorder.
By selecting the text and copying it into the clipboard. Click in the address bar, send Control-A and a Control-C characters, then just use the clipboard contents.

Resources