Using Selenium with AutoHotkey- Cross browser automation!

Helpful script writing tricks and HowTo's
User avatar
Xtra
Posts: 1271
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Oct 2018, 15:36

SeleniumBasic is a Visual Basic selenium wrapper program providing the COM interface method that AHK uses to control it.
User avatar
jeeswg
Posts: 5445
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using Selenium with AutoHotkey- Cross browser automation!

29 Oct 2018, 21:08

- I wrote a script to get the webpage titles/urls in Chrome v69.
- Some issues: it doesn't know how many tabs there are, the order is unspecified, it activates each tab (although very quickly).
- If anyone can improve this, I would be grateful.

Code: Select all

;[Google Chrome + SeleniumBasic installation instructions]
;Using Selenium with AutoHotkey- Cross browser automation! - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=7&t=32323&p=151996#p151996

w:: ;chrome + selenium basic - open urls
Loop, 3
{
	Run, % "chrome.exe --remote-debugging-port=9222 https://en.wikipedia.org/wiki/" Chr(64+A_Index)
	Sleep, 800
}
return

q:: ;chrome + selenium basic - get titles/urls
vOutput := ""
oDriver := ChromeGet()

Loop, 3
{
	vOutput .= oDriver.Window.Title "`r`n" oDriver.Url "`r`n`r`n"
	oDriver.SwitchToNextWindow ;change tab
}

MsgBox, % Clipboard := RTrim(vOutput, "`r`n") "`r`n"
return

;[ChromeGet function]
;note: start Chrome with: chrome.exe --remote-debugging-port=9222
;Using Selenium with AutoHotkey- Cross browser automation! - Page 2 - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=7&t=32323&p=181299#p181299

;slight modification by jeeswg of ChromeGet() by tmplinshi
ChromeGet(IP_Port:="127.0.0.1:9222")
{
	local
	driver := ComObjCreate("Selenium.ChromeDriver")
	driver.SetCapability("debuggerAddress", IP_Port)
	driver.Start()
	return driver
}
- Links:
[post by CH HAN and response by malcev]
Using Selenium with AutoHotkey- Cross browser automation! - Page 4 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 49#p231849
[get tab count and other tab info]
Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26947

- Btw is anyone having any success with Firefox and SeleniumBasic? It says here that people have to downgrade to Firefox v46.0.1 to use it with SeleniumBasic.
FireFox, Chrome, Edge, Opera Drivers are not working · Issue #133 · florentbr/SeleniumBasic · GitHub
https://github.com/florentbr/SeleniumBasic/issues/133
- Also, it looks like there has been no activity since Mar 2, 2016.
Release SeleniumBasic v2.0.9.0 · florentbr/SeleniumBasic · GitHub
https://github.com/florentbr/SeleniumBa ... g/v2.0.9.0
malcev
Posts: 112
Joined: 12 Aug 2014, 12:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

30 Oct 2018, 23:57

it doesn't know how many tabs there are
You can do it like this:

Code: Select all

loop % oDriver.Windows.Count
      msgbox % oDriver.Windows.Item(A_Index).Title "`n" oDriver.Url
it activates each tab
A I know it is impossible to get info from tab without activating it using selenium.
the order is unspecified
The order does not depend on position order, but depends from switch order.
For example if You switch 1-2-3, tab index of number 3 will be 1 and number 1 will be 3.
As a workaround You can compare each tab position through iaccessible interface.
shipaddicted
Posts: 15
Joined: 15 Jul 2016, 19:57

Re: Using Selenium with AutoHotkey- Cross browser automation!

12 Nov 2018, 17:44

I just started messing with this Selenium stuff last night. So far, it works great. I'm hoping I'll be allowed to use it on my work computer to automate a bunch of the useless crap we have to go through to complete the simplest of tasks. Anyhow, I'm trying to mash up a couple of scripts from earlier in this thread and I'm having a little trouble getting what I want. When I run this, I want it to first look and see if I already have Chrome open, and if so, use the existing window. If Chrome is not currently running, run it and go to the site I need. This is what I have:

Code: Select all

#!2::
If WinNotExist, ahk_exe chrome.exe {
	Run,chrome.exe --remote-debugging-port=9222
	}
	Else {
	WinActivate, ahk_exe chrome.exe
	}
driver := ComObjCreate("Selenium.ChromeDriver")
driver.SetCapability("debuggerAddress", "127.0.0.1:9222")
driver.Get("https://www.bing.com")
MsgBox, % driver.Title
Return


If I already have Chrome running, it works okay -- it goes to my most recently tab and opens the new url there (which is not really ideal -- I'd rather open a new tab in most cases, but I'll deal with that part later). But if I don't have Chrome running yet, nothing happens. If I manually open Chrome after trying to run the script, the script will pick up at that point and hijack a tab.

I have tried every version of the if/else statement above I can think of -- If !WinExist, switching the order of the statements, only having one of the statements (If but no else).
shipaddicted
Posts: 15
Joined: 15 Jul 2016, 19:57

Re: Using Selenium with AutoHotkey- Cross browser automation!

12 Nov 2018, 17:47

Immediately after posting I thought of another thing to try -- IfWinNotExist -- which worked. I wasn't going to bother with it since the documentation said it's deprecated, but Win[Not]Exist() didn't work either, so I thought, why not?

So now I at least have this functioning pretty well:

Code: Select all

#!2::
IfWinNotExist, ahk_exe chrome.exe 
	{
	Run,chrome.exe --remote-debugging-port=9222
	}
	Else 
	{
	WinActivate, ahk_exe chrome.exe
	}
driver := ComObjCreate("Selenium.ChromeDriver")
driver.SetCapability("debuggerAddress", "127.0.0.1:9222")
driver.AddArgument("--user-data-dir=C:\Users\Paige\AppData\Local\Google\Chrome\User Data\Default")    ; Use local chrome profile
driver.AddArgument("disable-infobars") ;disable notification "chrome is being controlled by automated test software"
driver.SetProfile("C:\Users\Paige\AppData\Local\Google\Chrome\User Data\Default")  ;set profile path
driver.Get("https://www.bing.com")
MsgBox, % driver.Title
Return
hotkeyguy
Posts: 127
Joined: 11 Oct 2014, 12:22

Re: Using Selenium with AutoHotkey- Cross browser automation!

18 Nov 2018, 17:37

Hello,

there is a promising new toy, especially for (but not only) Firefox users (like me):
intoli/remote-browser: A low-level browser automation framework built on top of the Web Extensions API standard.
And it can connect to an already running browser!

No further infos yet, I'm too excited! :superhappy:


Greetings
hotkeyguy
User avatar
adegard
Posts: 66
Joined: 24 Nov 2017, 05:58
GitHub: adegard

Re: Using Selenium with AutoHotkey- Cross browser automation!

21 Nov 2018, 06:53

Hi @Joe Glines

Thanks you for all this stuff you give on threads and website...
I create some functions to simplify IE automantion: https://autohotkey.com/boards/viewtopic ... 27#p249027
Could be usefull for someone here
Ceruleancerise
Posts: 4
Joined: 26 Sep 2017, 19:10

Re: Using Selenium with AutoHotkey- Cross browser automation!

02 Dec 2018, 14:35

Hi,

I've been following along with Joe Glines' tutorial and have installed SeleniumBasic and chromedriver under Joe's direction and have them under /Program Files/.
However, when I try to run a test script to launch chrome (See: https://p.ahkscript.org/?p=c9351c88) I get an error on line 4 that states "The system cannot find the file specified"

Why is this? Have I missed something? Thanks!
User avatar
Xtra
Posts: 1271
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

02 Dec 2018, 18:42

Ceruleancerise wrote:
02 Dec 2018, 14:35
Why is this? Have I missed something? Thanks!

Code: Select all

#SingleInstance, Force
Del::

driver := ComObjCreate("Selenium.ChromeDriver")           ; Start with Chrome
driver.Get("http://duckduckgo.com/")                      ; Navigate to a webpage

MsgBox here
return
Ceruleancerise
Posts: 4
Joined: 26 Sep 2017, 19:10

Re: Using Selenium with AutoHotkey- Cross browser automation!

02 Dec 2018, 22:12

Code: Select all

#SingleInstance, Force
Del::

driver := ComObjCreate("Selenium.ChromeDriver")           ; Start with Chrome
driver.Get("http://duckduckgo.com/")                      ; Navigate to a webpage

MsgBox here
return
same result :/

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 5 guests