Using Selenium with AutoHotkey- Cross browser automation!

Helpful script writing tricks and HowTo's
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Using Selenium with AutoHotkey- Cross browser automation!

26 May 2017, 15:38

I finally got around to documenting some of the work I've done using AutoHotkey and Selenium. :superhappy:
On this page I have several videos & example code showing what you need to install, and now to use the code. I hope y'all find this helpful! It was incredibly confusing!

While all the example code is on the above links, you can see the videos below:

What is Selenium (and why does it matter for AutoHotkey users)- No longer be tied to just automating IE!!!


Installing SeleniumBasic for use with AutoHotkey (When you try and install Selenium on your own you'll understand how confusing it is!)


Starting up the WebDrivers & navigating to a page (BTW- you don't have to "wait" for the page to load!)


Getting information from the page (somewhat similar to AutoHotkey & COM but there are significant differences)


Setting text and clicking links (This is very different than AutoHotkey & IE. Hopefully this can be improved upon!)
Please note: be very careful you watch your case as the code is case sensitive!


Here I demonstrate how you can leverage your Chrome profile to avoid logging in each time! :headwall:


When iterating over Objects in Selenium, please remember that the objects are in the Keys, not the values.
Two other important notes are: use .Count instead of .Length and you don't need to use .item[A_index] inside a for loop.


Here I review quite a few methods available in Selenium for getting/setting text on a page. CSS & XPath take some getting used to but can be invaluable when you are trying to grab very specific things. :clap:


In this video I demonstrate some addtional cool Selenium Methods as well as some equivalents by injecting JavaScript.
Be sure to check out my site posting for the actual AutoHotkey code.


Frames are always such a pain I figured I should at least touch on how to navigate them with Selenium & AutoHotkey. :headwall:


Downloading files (PDF, Excel, etc) with Selenium & AutoHotkey is pretty easy.
Last edited by Joe Glines on 30 May 2017, 10:44, edited 6 times in total.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Using Selenium with AutoHotkey- Cross browser automation!

27 May 2017, 15:26

That's a great tutorial I wish I had when I started using it.
Recommends AHK Studio
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

27 May 2017, 17:49

Thanks! Ya, I don't actually plan on using Selenium much (as I'm ok with IE) but it was a bear so I wanted to share what I'd figured out. I have more to go but this was a decent start
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
guest3456
Posts: 3453
Joined: 09 Oct 2013, 10:31

Re: Using Selenium with AutoHotkey- Cross browser automation!

28 May 2017, 16:39

i was not aware of any of this Selenium stuff, thanks

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

30 May 2017, 10:33

Can't say I blame you there. It has not gotten much notice on the forum and there was little documented. Personally I don't plan to use it much as I'm okay just automating with IE via a COM object but many others complain about having to use IE so I thought this would be something that many would be interested in.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using Selenium with AutoHotkey- Cross browser automation!

30 May 2017, 11:48

Is it relatively easy to get the webpage titles and urls for all tabs (in IE/Firefox/Chrome/Edge), or to identify the active tab?

Also, do you think IE + Selenium, can do everything that IE + COM can do, or are there limitations? Plus if you can inject JavaScript, does that in theory mean that you can do everything with IE that COM can do, with Selenium?

Many thanks for your tutorials and website.

[What I have so far, without Selenium:]
Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14#p139114
[Internet Explorer, get tab names (webpage titles) and urls]
[and of course there are all sorts of things you can do with IE and COM]
Internet Explorer: get WB hWnds via object loop (+ do win objects have unique object IDs) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 38#p125038
Selenium is a chemical element with symbol Se and atomic number 34.
==================================================

Introduction — Selenium Documentation
http://www.seleniumhq.org/docs/01_intro ... lenium.jsp
Selenium-WebDriver

Selenium-WebDriver supports the following browsers along with the operating systems these browsers are compatible with.
•Google Chrome
•Internet Explorer 7, 8, 9, 10, and 11 on appropriate combinations of Vista, Windows 7, Windows 8, and Windows 8.1. As of April 15 2014, IE 6 is no longer supported. The driver supports running 32-bit and 64-bit versions of the browser where applicable
•Firefox: latest ESR, previous ESR, current release, one previous release
•Safari
•Opera
•HtmlUnit
•phantomjs
•Android (with Selendroid or appium)
•iOS (with ios-driver or appium)
Note: I don't see Edge there.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

30 May 2017, 21:28

@jeeswg
1) Selenium and COM have different features / functionalities. I would not put one above the other, just note that they are different

2) as for getting active tabs- In Selenium I don't belive you can connect to an existing browser (unless it was launched with Selenium). Thus you'll need to be planning ahead if you wish to "connect" to an existing tab.

3) the WebDrivers are now created by the browsers not Selenium. During our last webinar someone pointed out there was a webdriver for Edge. After the webinar blackholyman and I played with the webdriver and were able to connect to it via COM (without Selenium). I could be wrong on this but I believe the webdrivers are going to be the new "COM" that we can connect to with/without Selenium.

4) you can inject JavaScript with IE/COM as well. Perhaps you knew this but just wanted to state it here for others...
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Using Selenium with AutoHotkey- Cross browser automation!

30 May 2017, 22:43

Thanks a lot, Joe, for your infos on the subject. Your videos will surely help me to get an overview about the capabilities of Selenium and how it might help me in future projects.
I was aware of Selenium and had glanced at it before, but I never managed to get past the first hurdles. I just started with your videos and they are already very helpful and time-saving!! Much appreciated!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

31 May 2017, 06:47

@gregster: You bet! There were some interesting nuances that, over time, I'd figured out so I wanted to share back to help others. :)
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using Selenium with AutoHotkey- Cross browser automation!

03 Jun 2017, 23:21

I had a little go on SeleniumBasic:

Code: Select all

;downloaded SeleniumBasic (22 MB)
;(note: did *not* need to separately download Selenium)
;https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
;SeleniumBasic-2.0.9.0.exe
;note: it installed to:
;C:\Users\%username%\AppData\Local\SeleniumBasic

;downloaded ChromeDriver 2.29
;https://sites.google.com/a/chromium.org/chromedriver/downloads
;chromedriver_win32.zip
;note: move new version of chromedriver.exe to install folder:
;e.g. C:\Users\%username%\AppData\Local\SeleniumBasic

;IE and Chrome worked, Mozilla timed out

q::
vBrowser := "ie"
vBrowser := "firefox"
vBrowser := "chrome"

;driver:= ComObjCreate("Selenium.WebDriver")
if (vBrowser = "ie")
	driver:= ComObjCreate("Selenium.IEDriver")
if (vBrowser = "firefox")
	driver:= ComObjCreate("Selenium.FireFoxDriver")
if (vBrowser = "chrome")
	driver:= ComObjCreate("Selenium.CHROMEDriver")

;Start doesn't navigate until the Get
driver.Start(vBrowser,"http://duckduckgo.com/")
driver.Get("/")

Sleep 5000

driver.Get("http://the-automator.com/")
MsgBox % driver.findElementByID("site-description").Attribute("innerText")

MsgBox, % "done"

;reload script closes the browser
return
Btw:
- Worked with IE.
- Mozilla timed out when I tested.
- Worked with Chrome, although it had this label saying 'Chrome is being controlled by automated test software', it can be closed manually, but can it be prevented from being shown?
- Re. Edge: an 'edgedriver.exe' came with SeleniumBasic (but I can't test it because I'm on Windows 7).

- Can you have multiple tabs per driver object?
- I didn't need any additional files other than the 2 I mentioned, however when I clicked vbsc.exe, it just said: 'VisualBasic Script Host Console' 'Preface with '?' for an evaluation.'.
- On the SeleniumBasic install, I unticked 'Install the Selenium IDE Addon for Firefox'.
- My IE 11 (Win 7) can't always cope with the YouTube links on this page, not sure what the issue is.
Thanks again.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

05 Jun 2017, 20:27

'Chrome is being controlled by automated test software', it can be closed manually, but can it be prevented from being shown?

Code: Select all

driver := ComObjCreate("Selenium.ChromeDriver")     ; Start with Chrome
driver.AddArgument("disable-infobars")              ; <---
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

05 Jun 2017, 20:41

Thanks @Xtra! I was receiving a different window. Hopefully this will prevent it from popping up!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

06 Jun 2017, 08:31

please note there is a distinction to be had. COM allows communication method with an object and its children. Selenium is a automated web testing driver. you will have more flexibility with COM and IE. Automated testing lends itself to automation but the goals are different. Single browser and interaction with elements of the page.. In general Selenium is good for automation where you will with your script own the entire life-cycle of the browser and a single tab. I dont recall selenium handing multiple tabs but my memory could be faltering
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
fischgeek
Posts: 433
Joined: 29 Jan 2014, 21:39

Re: Using Selenium with AutoHotkey- Cross browser automation!

06 Jun 2017, 10:44

This is pretty cool. I use Selenium in my unit tests for .net web projects, but never thought of leveraging it for ahk. Nice!
A_User
Posts: 36
Joined: 21 Aug 2017, 01:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

23 Aug 2017, 10:29

Very helpful. Thank you.

One thing. In order to automate Chrome, I had to update chromedriver.exe by replacing %LOCALAPPDATA%\SeleniumBasic\chromedriver.exe with the one which can be downloaded from here. Otherwise, an error occurred.
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: Using Selenium with AutoHotkey- Cross browser automation!

05 Sep 2017, 11:15

Hey Joe!

First off, thanks for all your other COM Tutorials as well as this one :) i use COM everyday now.

I got Selenium up and running with AHK - and my first issue was not being able to connect to an exisiting instance of chrome - which as you stated doesnt look like can be done.

However, i came across this post:
https://stackoverflow.com/questions/187 ... -in-python

Looks like perhaps it can be done? Let me know what you think! I'm going to keep playing around.

Thanks again!
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

18 Sep 2017, 02:40

Hi,

I'd like to thank Joe about this great tutorial. It's so good to me cause a web page I want to control has renewal it's setting, so I can't use COM anymore.
Even all good, but the most important thing I want is not works, which is using Chrome profile. I need to control the web page that requests me login every time.
I put chrome://version/ in Chrome to get the profile path and use it in the code you gave us, but new Chrome web pages keep asking me re-login. I've googled about chrome profile, and tried almost all ways what I got from searching, but it still not working. I even tried this weird code that

Code: Select all

driver.executeScript("driver.addArguments = 'user-data-dir=C:\Users\Hahn\AppData\Local\Google\Chrome\User Data\Default'")

Now I'm totally frustrated using selenium, so thinking of ImageSearch command which I really don't want to use.

Thank you, and I look forward to meet someone who can solve this problem.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

19 Sep 2017, 05:21

Hi Marcus, I just re-tested my code and it worked as expected. (I had to login once with the chrome version that launched with Selenium and then reload but the second time it stayed logged-in). Are you sure you got the location of your chrome profile? Is Chrome up to date?

Also, instead of using ImageSearch, you might check out this tutorial where I use this function. It works similar to ImageSearch however you don't need to have a file and is very quick to develop with.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

19 Sep 2017, 11:24

@CH HAN Try using:

Code: Select all

driver.AddArgument("--user-data-dir=C:\Users\Hahn\AppData\Local\Google\Chrome\User Data\Default")    ; Use local chrome profile
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

19 Sep 2017, 21:52

/Joe Glines
Yes, I got the path from Chrome. Actually I copied exactly what you've shown on the demo video. The version of my Chrome is 61.0.3163.91 Now I'm testing your FindText, it's really good. :thumbup:

/Xtra
I've tried the code above, but it doesn't work :(

It's my code from yours.

Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver

driver.SetProfile("C:\Users\Hahn\AppData\Local\Google\Chrome\User Data\Default") ; 'Full path of the profile directory
;~ driver.AddArgument("--user-data-dir=C:\Users\Hahn\AppData\Local\Google\Chrome\User Data\Default")

url:="https://www.facebook.com/"
driver.Get(url)

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 26 guests