Using Selenium with AutoHotkey- Cross browser automation!

Helpful script writing tricks and HowTo's
User avatar
Xtra
Posts: 1478
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: 6844
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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
malcev
Posts: 268
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: 27
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: 27
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: 146
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: 77
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: 1478
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 :/
User avatar
Sabestian Caine
Posts: 420
Joined: 12 Apr 2015, 03:53

Re: Using Selenium with AutoHotkey- Cross browser automation!

23 Dec 2018, 03:35

Joe Glines wrote:
08 Dec 2017, 19:48
@CH HAN- thanks for publishing that! I made some minor tweaks and demonstrate it's usage here. :D


Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.Get("http://the-automator.com")
;********************find and click text***********************************
keyword:="Continue reading"  ; keyword you want to find- Note Case sensitive

if(driver.FindElementByXPath("//*[text() = '" keyword "']"))
	driver.FindElementByXPath("//*[text() = '" keyword "']").click()
Hello dear Joe Glines.

Sir, I am trying to automate chrome by selenium and i want to get the URL from a button or link in a webpage. I am unable to retrieve the URL from a specific button or link in a webpage. For this i have asked a question in AutoHotKey help form. The link of my question is this-

https://autohotkey.com/boards/viewtopic ... 76&t=60232


I request you to please solve this issue. Thanks a lot sir... :D :D
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
User avatar
Sabestian Caine
Posts: 420
Joined: 12 Apr 2015, 03:53

Re: Using Selenium with AutoHotkey- Cross browser automation!

23 Dec 2018, 06:36

Joe Glines wrote:
08 Dec 2017, 19:48
@CH HAN- thanks for publishing that! I made some minor tweaks and demonstrate it's usage here. :D


Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.Get("http://the-automator.com")
;********************find and click text***********************************
keyword:="Continue reading"  ; keyword you want to find- Note Case sensitive

if(driver.FindElementByXPath("//*[text() = '" keyword "']"))
	driver.FindElementByXPath("//*[text() = '" keyword "']").click()
Problem solved sir!!!!

this is working-

Code: Select all

MsgBox % driver.findElementsByClass("forumtitle").item[1].Attribute("href") 
Thanks a lot sir..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
inseption86
Posts: 74
Joined: 19 Apr 2018, 00:24

Re: Using Selenium with AutoHotkey- Cross browser automation!

07 Apr 2019, 09:07

Please help...

Code: Select all

driver := ComObjCreate("Selenium.ChromeDriver")
driver.AddArgument("--disable-infobars")
driver.AddArgument("--start-maximized") 

driver.Get("https://www.google.com")

driver.ExecuteScript("window.open();")
driver.SwitchToNextWindow
driver.Get("https://yandex.ru")


w::

;"https://www.google.com"

 MsgBox %  driver.findElementsByTag("input").item[2].Attribute("value")
 return
 
q::

; "https://yandex.ru"
   MsgBox % driver.findElementsByTag("input").item[2].Attribute("value")
 return
flatwater
Posts: 7
Joined: 28 Dec 2017, 08:21

Re: Using Selenium with AutoHotkey- Cross browser automation!

08 May 2019, 03:57

How do I click elements inside an iframe?
User avatar
Xtra
Posts: 1478
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

08 May 2019, 12:45

flatwater wrote:
08 May 2019, 03:57
How do I click elements inside an iframe?
use:

Code: Select all

driver.SwitchToFrame("iframeNameHere") 
And then click the element like normal.

The trick is to always start at the base document and switch to the next frame and then the next (if needed) you can not jump over iframes/frames.
Imagine a tree that you have to go from the trunk and up every branch to your destination but you cant jump across branches,

When you want to go back to the base document use:

Code: Select all

driver.SwitchToDefaultContent()
Overall its very simple compared to IE.

HTH
flatwater
Posts: 7
Joined: 28 Dec 2017, 08:21

Re: Using Selenium with AutoHotkey- Cross browser automation!

08 May 2019, 19:09

Xtra wrote:
08 May 2019, 12:45
flatwater wrote:
08 May 2019, 03:57
How do I click elements inside an iframe?
use:

Code: Select all

driver.SwitchToFrame("iframeNameHere") 
And then click the element like normal.

The trick is to always start at the base document and switch to the next frame and then the next (if needed) you can not jump over iframes/frames.
Imagine a tree that you have to go from the trunk and up every branch to your destination but you cant jump across branches,

When you want to go back to the base document use:

Code: Select all

driver.SwitchToDefaultContent()
Overall its very simple compared to IE.

HTH
Thank you very much. The iframe doesn't have a name but it is the only iframe in the page. I use driver.SwitchToFrame(0) to switch to the frame and it works. Thanks.
warwickc
Posts: 1
Joined: 11 Jul 2019, 02:19

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Jul 2019, 02:29

Hi

Loving Selenium, know I've learnt xPath it's really powerful. Joe, your tutorials have been fantastic help over a very long night!

Couple of questions
How do you handle Pop-Up Alerts. I've seen this:

Code: Select all

driver.switchTo().alert().accept();
but think thats java as it doesnt work in AHK, and I cant find the corresponding command to use in AHK.

Also, can you set an implicit wait. Again, according to https www.guru99.com /implicit-explicit-waits-selenium.html Broken Link for safety it should look like

Code: Select all

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
Any help much appreciated - my project is nearly there but it keeps timing out, and I cant deal with those pop ups!
W
malcev
Posts: 268
Joined: 12 Aug 2014, 12:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Jul 2019, 07:40

Just search Seleniumbasic manual for your commands.
mikea105
Posts: 8
Joined: 04 Nov 2016, 11:51

Re: Using Selenium with AutoHotkey- Cross browser automation!

12 Jul 2019, 16:56

I normally use an explicit wait. The syntax below seems to work fine with Autohotkey and Selenium. This line waits up to 30 seconds for the element to appear and be clickable. Hopefully it will work for you.

myDynamicElement := (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(driver.executeScript("document.getElementById('ScanButton')")))
AHK_user
Posts: 30
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Using Selenium with AutoHotkey- Cross browser automation!

24 Sep 2019, 15:53

I was trying to open a new tab, but the popup blocker blocks it.
I tried adding the disable-popup-blocking, but without succes.
Does anybody knows how to disable the popup blocking?


driver.AddArgument("--disable-popup-blocking")
driver.ExecuteScript("window.open();")

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 7 guests