VBA Selenium Sendkeys Modifier Keys - vba

I'm trying to use Sendkeys for Ctrl+Shift+B but
.Sendkeys ("^+B")
is not working.

Try creating a Keys object:
Dim keyObj As Selenium.Keys
Set keyObj = New Selenium.Keys
bot.SendKeys keyObj.Control & keyObj.Shift & "B"

Related

Selenium Zoom with VBA

I am looking for a way to control Zoom in chrome with selenium.
I have tried
driver.ExecuteScript ("document.body.style.zoom='75%'")
This zoom in the actual page, NOT like ctrl & -
driver.ExecuteScript "chrome.settingsPrivate.setDefaultZoom(1.5)"
This wont complie
I have no problems with eg. CTRL+a
(for selecting evrything
driver.FindElementByTag("body").SendKeys Keys.Control, "a"
but it dosn't work for SendKeys Keys.Control, Subtract
The second command doesn't compile because before using it you have to go to the settings page
driver.Get('chrome://settings/')
driver.ExecuteScript('chrome.settingsPrivate.setDefaultZoom(1.5);')
Login Zoom and open multiple pages.
First- dont Zoom before login, it wont click the BTN, heres my final code
Dim driver As New Selenium.ChromeDriver ' defines outside sub to prevent Crome to close at end of sub
sub loginOpen()
driver.start "chrome", "" ' new driver instance
Dim sURL As String
Dim rng As Range
Dim First As Boolean
Dim i As Variant
First = True
Set rng = Selection
With driver
For Each i In rng
If First Then
.Get sURL ' could also just be the login page
.Window.SetSize 1400, 768
First = False
' if we need to login
If Left(.Url, Len(sURL)) <> sURL Or .Url = "" Then
.FindElementByName("tbusername").SendKeys ("user")
.FindElementByName("tbpassword").SendKeys ("password")
.FindElementById("btnlogin").Click
.Get ("chrome://settings/")
.ExecuteScript ("chrome.settingsPrivate.setDefaultZoom(0.75);")
.Get sURL
End If
Else
.ExecuteScript "window.open(arguments[0])", sURL
.SwitchToNextWindow
End If
Next
End With
End Sub

How to use FindEelementById in Excel VBA

I want to open the google chrome and search "Christmas date" using excel VBA. The code I am using:
Sub test544()
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url chrome-extension://dehlblnjjkkbapjemjbeafjhjpjoifii/index.html")
End Sub
It opens chrome. Now I am trying to search "Christmas Date" on chrome by FindElementById and SendKeys as follows:
chromePath.FindElementById("keywords").SendKeys ("Christmas date")
chromePath.FindElementById("Button").Click
Which shows the following error
Is there any way to do that?
Instead of this :
chromePath.FindElementById("keywords").SendKeys ("Christmas date")
chromePath.FindElementById("Button").Click
do this :
chromePath.FindElementById("keywords").SendKeys "Christmas date"
chromePath.FindElementById("Button").Click
Is button a unique identifier in your application ?
Update 1 :
Dim bot As New WebDriver
bot.Start "chrome", "http://google.com"

Fill proxy form when PC is locked

I need to enter a site for which a proxy form has to be filled. I couldn't get through the form using Selenium VBA.
I called a VBS script from VBA to fill the form. It works when the PC is not locked.
Below is the proxy form.
Outlook VBA code to call VBS:
Dim bot as new chromedriver
bot.Start "chrome", "https://nissan.service-now.com/nav_to.do?uri=%2Fhome.do%3F"
bot.Get ("https://nissan.service-now.com/nav_to.do?uri=%2Fhome.do%3F") '//load webpage
strFileName = "d:\LocalData\Z018439\Desktop\MY\NX-AMO\VBACodes\Shell.vbs"
Set oshell = CreateObject("Wscript.shell")
oshell.Run "vbsc " & strFileName '//call vbs file
VBS code to fill proxy form:
Sub test()
Dim shell_object
Dim app_path, time
wscript.Sleep 12000
shell_object = "wscript.shell"
Set objshell = CreateObject(shell_object)
objshell.SendKeys ""
objshell.SendKeys "{Tab}"
objshell.SendKeys ""
objshell.SendKeys "{Enter}"
End Sub
When the PC is locked, keyboard function is disabled and hence sendkeys(VBS) is not working. Launch is done upon arrival of a new mail in Outlook.
How do I fill the proxy form when the PC is locked.

Extract list of all input boxes on webpage vba

I want to create a list on Excel of all the labels of input boxes on a webpage- so I imagine the code would be something like:
Sub IEInteract()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "mywebsite.com"
IE.Navigate URL
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
objCollection = IE.Document.getElementsByTagName("input")
For Each el In objCollection
label = el.label 'or something like that????'
Debug.Print label
Next el
End Sub
Where am I going wrong? Thanks
BTW My VBA is OK, but my HTML is non-existent.
For learning purposes maybe choose a website that has more obvious inputboxes, rather than dropdowns.
Many inputboxes won't be pre-populated so maybe consider reading other properties of the retrieved elements. Or even writing to them and then retrieving those values.
Selecting by tag name can bring back a host of items that you might not have expected.
Bearing all of the above in mind. Try running the following, which generates a collection of <input> tag elements.
Code:
Option Explicit
Public Sub PrintTagInfo()
'Tools > references > Microsoft XML and HTML Object library
Dim http As New XMLHTTP60 '<== this will be specific to your excel version
Dim html As New HTMLDocument
With http
.Open "GET", "https://www.mrexcel.com/forum/register.php", False
.send
html.body.innerHTML = .responseText
End With
Dim inputBoxes As MSHTML.IHTMLElementCollection, iBox As MSHTML.IHTMLElement, i As Long
Set inputBoxes = html.getElementsByTagName("input") '<== the collection of input tags on the page
'<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
For Each iBox In inputBoxes
Debug.Print "Result #" & i + 1
Debug.Print vbNewLine
Debug.Print "ID: " & iBox.ID '<== select a sample of properties to print out as some maybe empty
Debug.Print "ClassName: " & iBox.className,
Debug.Print "Title: " & iBox.Title
Debug.Print String$(20, Chr$(61))
Debug.Print vbNewLine
i = i + 1
Next iBox
End Sub
Sample output:
From the above, it looks like class name might be in some ways more informative if you are looking to target boxes to input information into.
An initial inspection of the page source, selecting an inputbox and right-click > inspect... will help you refine your choices.
I noticed that a lot of the boxes of interest had the Input tag and then type = "text"
This means you can target elements matching this pattern using CSS selectors. In this case using the selector input[type=""text""].
Adjusting the former code to factor this in gives a smaller set of more targeted results. Note, using .querySelectorAll, to apply the CSS selector, returns a NodeList object which requires a different method of iterating over. A For Each Loop will cause Excel to crash as described here.
Code:
Option Explicit
Public Sub PrintTagInfo()
'Tools > references > Microsoft XML and HTML Object library
Dim http As New XMLHTTP60 '<== this will be specific to your excel version
Dim html As New HTMLDocument
With http
.Open "GET", "https://www.mrexcel.com/forum/register.php", False
.send
html.body.innerHTML = .responseText
End With
Dim inputBoxes As Object, i As Long
Set inputBoxes = html.querySelectorAll("input[type=""text""]") '<== the collection of text input boxes on page. Returned as a NodeList
'<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
For i = 0 To inputBoxes.Length - 1
Debug.Print "Result #" & i + 1
Debug.Print vbNewLine
Debug.Print "ID: " & inputBoxes.Item(i).ID '<== select a sample of properties to print out as some maybe empty
Debug.Print "ClassName: " & inputBoxes.Item(i).className,
Debug.Print "Title: " & inputBoxes.Item(i).Title
Debug.Print String$(20, Chr$(61))
Debug.Print vbNewLine
Next i
End Sub
Sample results:
Note: I have edited the spacing to fit more into the image.
References added via VBE > Tools > References
Last two are those of interest. The bottom one will be version specific and you will need to re-write XMLHTTP60 which is for XML 6.0 to target your version of Excel if not using Excel 2016.

VBA-Selenium run time error, window not found: unable to find element on closed window

however I tried, VBA selenium can't find existing element in a loaded page.
Here is the last code I use:
Sub TableData()
Dim driver As New WebDriver
Dim sInfo As String
With driver
.Start "Internet Explorer"
.Get "http://cvb.manatron.com/Tabs/PropertySearch.aspx"
.FindElementById("fldInput").SendKeys ("Carter")
.FindElementById("btnsearch").Click
sInfo = .FindElementById("grm-search", timeout:=20000).Text
Debug.Print sInfo
.Quit
End With
End Sub
Hope somebody can help. Thanks
This worked for me by adding a delay before the SendKeys and attempting to retrieve sInfo.
Option Explicit
Sub TableData()
Dim driver As New WebDriver
Dim sInfo As String
With driver
.Start "Chrome" '"Internet Explorer"
.Get "http://cvb.manatron.com/Tabs/PropertySearch.aspx"
Application.Wait Now + TimeValue("00:00:02")
.FindElementByCss("#fldInput").SendKeys "Carter"
.FindElementById("btnsearch").Click
Application.Wait Now + TimeValue("00:00:01")
sInfo = .FindElementById("grm-search", timeout:=20000).Text
Debug.Print sInfo
.Quit
End With
End Sub
Output: