Send Keys from VBA to chrome by Chrome DevTools Protocol - excel

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.

Related

How to copy text from a web element division having "CALSS" tag plus "Data-Role" tag in the same division

I am trying to copy the text highlighted in Yellow (from the HTML image) using a Selenium VBA code with MS Excel to a string. However, it is crashing and not getting copied. But the same code is working if I were to use it on other websites where the division does not contain "Data-Role" element within a webpage. I tried to make use of all element type like ByName ById ByClass but none of these technique seems to be working. Please note I am using Selenium to access the Chrome browser. I am trying to store the Yellow highlighted text data into a string "Res". Any help or tip is much appreciated. Below is the sample code which I am trying to use.
Dim Res As String
Dim be As New WebDriver
On Error Resume Next
be.Start "chrome", ""
be.Get "the website from which I am tryig to pull data"
Res = be.FindElementByClass("text--1jzYQ uppercase--tL_HU").Text
[![enter image description here][2]][2]
Without having the website address (if able to test with it), or any error messages, can't tell you why it is crashing. Possibly accessing a dynamic element which is still resolving? Dunno.
However, those classes look dynamic, so in terms of robustness, perhaps try using a combination of more stable looking attribute = value css selectors:
be.FindElementByCss("[data-role=status-bar] [data-role=status-text]").text

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

Screenshotting Google Maps and Pasting into Excel Document VBA [duplicate]

This question already has answers here:
is it possible to display a Google Earth map INSIDE Excel?
(3 answers)
Closed 4 years ago.
I have a code that already searches for the latitude and longitude and pastes to my worksheet, which works perfectly. I'm looking for a way to take that latitude and longitude, load google maps, and either take a screenshot of the google maps page or embed the map into Excel.
In my code below I have a code that already loads google maps for any input address, but I do not know how to either take the screenshot of the map (preferably without the input information on the side of the page) or embed the map into Excel. The extra code at the bottom is for a request/response from a USGS website that pulls official seismic information for a location, but should not effect the top part of the code.
Please note that I want this to just be a static screenshot of the map if possible. I do not want to install Google Earth on multiple desktops to be able to embed an interactive map into the worksheet if at all possible.
Option Explicit
Public Sub Seismicgrab()
Dim browser As New ChromeDriver
Dim URL As String
Dim ws As Object
Dim xmlhttp As New MSXML2.XMLHTTP60
browser.Get "http://www.google.com/maps?q=" & Range("H13").Value
browser.Wait 5000
Cells(19, 13).Value = browser.URL
browser.Close
URL = Range("M24").Value
xmlhttp.Open "GET", URL, False
xmlhttp.Send
Worksheets("Title").Range("M25").Value = xmlhttp.responseText
End Sub
You can use the TakeScreenshot method of the object
browser.TakeScreenshot.SaveAs ".....jpg" '<== put your path and file name here
For more flexibility e.g. cropping consider switching languages and using any of these methods:
How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
Additionally, there are ways I believe with standard VBA and API calls to take a screenshot and then crop an image.

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

Unable to click on the link using selenium and Python

Image Attached I want to click on the specific button on a webpage, using selenium webdriver and Python. Tried finding the element by CSS, class name and XPath, but it doesn't seem to work. I have attached an image showing the button I want to click (It is not a dropdown as the image suggests) and the HTML details. Any help would be appreciated.
Actually sometimes selenium is not able to interact with some web elements using click try simulating enter key press on that element for e.g. -
element = driver.find_element_by_id("value")
element.send_keys(:return)
Let me know if that works

Resources