Using Selenium with AutoHotkey- Cross browser automation!

Helpful script writing tricks and HowTo's
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Using Selenium with AutoHotkey- Cross browser automation!

09 Nov 2017, 03:46

Nice tutorial!

I want to add a useful tips here:

Connect to existing Chrome window just like WBGet()!

Step 1. Start Chrome with command line: chrome.exe --remote-debugging-port=9222
(Be sure to kill all running Chome windows that didn't start with --remote-debugging-port=9222)

Step 2. Open some webpages manually, then run the following code:

Code: Select all

driver := ChromeGet()
MsgBox, % driver.Window.Title "`n" driver.Url

ChromeGet(IP_Port := "127.0.0.1:9222") {
	driver := ComObjCreate("Selenium.ChromeDriver")
	driver.SetCapability("debuggerAddress", IP_Port)
	driver.Start()
	return driver
}

F12::
	driver.Start()
	MsgBox, % driver.Window.Title "`n" driver.Url
return
:beer:
Last edited by tmplinshi on 09 Nov 2017, 04:51, edited 1 time in total.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

09 Nov 2017, 04:19

@tmplinshi Thanks! That should fix the one thing i didnt like about selenium.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

09 Nov 2017, 06:29

@tmplinshi- Great work!!! I tested this and was able to get it to connect to an open Chrome window. Having said that the second part of your code (under F12 hotkey) doesn't work for me but I swapped it with the following and it worked fine. :)

Any idea on how to do this with FireFox and/or IE?

Code: Select all

F12::
driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.Get("http://the-automator.com/")
MsgBox, % driver.Window.Title "`n" driver.Url
return
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
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

09 Nov 2017, 09:29

Joe Glines wrote: Any idea on how to do this with FireFox and/or IE?
For Firefox we might be able to use the same mechanism via remote debugging port .... see: https://developer.mozilla.org/en-US/doc ... erver_port

Haven't tested it!
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Nov 2017, 16:01

Wow! Now I can use an existing browser and leave it opened after end of codes. Thank you!!

Code: Select all


UsingAnExistingBrowser()

Exitapp


UsingAnExistingBrowser(){
	
	URL = http://the-automator.com/

	run % "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222" ( winExist("ahk_class Chrome_WidgetWin_1") ? " --new-window " : " " ) URL 

	driver := ChromeGet()

	driver.findElementsByName("s").item[1].SendKeys("hello world")	
	driver.findElementsByName("s").item[1].SendKeys(driver.Keys.ENTER) ;http://seleniumhome.blogspot.com/2013/07/how-to-press-keyboard-in-selenium.html
	MsgBox pause

	return
}


ChromeGet(IP_Port := "127.0.0.1:9222") {
	driver := ComObjCreate("Selenium.ChromeDriver")
	driver.SetCapability("debuggerAddress", IP_Port)
	driver.Start()
	return driver
}
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Nov 2017, 16:54

Hi,

I want to click mouse right button on a element of a web page to select one of menu, so I googled and visited many pages, but almost solution was coded by Java, but because I don't know other computer languages I couldn't get it. I tried some codes, but it was not working.

Code: Select all

	driver.FindElementByXPath("//*[@id="contents-body"]/div/div[2]/table/tbody/tr[8]/td[2]/a").contextClick().build().perform()
	; driver.FindElementByXPath("//*[@id="contents-body"]/div/div[2]/table/tbody/tr[8]/td[2]/a").rightclick()
I think the answer is on a web page below at a section "13. Context Click (Right Click)", but I can't apply it to AHK

http://seleniumhome.blogspot.com/2013/1 ... -code.html
Last edited by CH HAN on 11 Nov 2017, 17:15, edited 3 times in total.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Nov 2017, 16:56

@ CH HAN You can combine your 2 SendKeys lines into one like this:

Code: Select all

driver.findElementsByName("s").item[1].SendKeys("hello world").SendKeys(driver.Keys.ENTER)
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Nov 2017, 17:10

@ Xtra
Thank you for quick answer. Your code is good. It can help me save code, but I want to 'CLICK' mouse right button on a specific element of a web page :)

And there is another problem (I changed my code above of my post while you uploaded your post.) which an error message is that The following variable name contains an illegal character: "body""

I added ` before " but it's not working.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

11 Nov 2017, 17:48

Use single quotes for xpath id like this:

Code: Select all

"//*[@id='contents-body']"
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

12 Nov 2017, 04:00

@ Xtra
Thank you, but it's not working...

Code: Select all


URL = http://the-automator.com/

run % "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222" ( winExist("ahk_class Chrome_WidgetWin_1") ? " --new-window " : " " ) URL 

driver := ChromeGet()

; To click 'Web Scraping'
driver.FindElementByXPath("//*[@id='menu-item-1662']/a/span").click()



ChromeGet(IP_Port := "127.0.0.1:9222") {
	driver := ComObjCreate("Selenium.ChromeDriver")
	driver.SetCapability("debuggerAddress", IP_Port)
	driver.Start()
	return driver
}
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

01 Dec 2017, 11:22

I'm curious if this will affect Selnium & ahk use
Google will lock down chrome on Windows
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!
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

02 Dec 2017, 18:33

Hi,

I've figured out a way to use Xpaths including ""(quotation-marks).

Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.Get("https://www.google.com/")

; Put a keyword 'AutoHotkey' in a blank
Xpath = //*[@id="lst-ib"]
driver.FindElementByXPath(Xpath).SendKeys("AutoHotkey")
driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Enter)

MsgBox

; Open AutoHotkey web page
Xpath = //*[@id="rso"]/div[1]/div/div/div/div/h3/a
driver.FindElementByXPath(Xpath).click()

MsgBox
If you want to make your own customized Xpaths, you should check a video below.

https://youtu.be/3uktjWgKrtI
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

02 Dec 2017, 18:48

R_google_drive.jpg
R_google_drive.jpg (78.09 KiB) Viewed 18648 times
I still don't know how I open right-click menu using Selenium. Even I tried to use COM to open a right-click menu but all methods I tried has failed.

Code: Select all

wb := IEGet("My Drive - Google Drive - Internet Explorer")

; everything is not working
/*
wb.document.getElementsByTagName("SPAN")[84].focus()
Send, {AppsKey}            
Send, {Down}
Send, {Down}
Send, {Enter}
*/

;~ wb.document.getElementsByTagName("SPAN")[84].click(Right)
;~ wb.document.getElementsByTagName("SPAN")[84].rightclick()

MsgBox
I've searched many times and many web pages to figure out ways to click mouse right button on Chrome but I couldn't find any ways. I think I should learn Java and run Selenium on it.

https://seleniumhome.blogspot.com/2013/ ... -code.html
https://stackoverflow.com/questions/114 ... river-java

Does anybody know any ways to open right-click menu and select one of elements of the menu on Chrome using Selenium~?!?!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

02 Dec 2017, 19:57

I wouldn't try and send mouse-clicks. Are the actions you want taken available from the HTML on the element? Typically they are a you can just trigger the proper event (or load the proper url, etc)
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!
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

03 Dec 2017, 19:26

Code: Select all

#persistent
driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.Get("https://www.google.com")
element := driver.FindElementById("lst-ib")
driver.Actions.ClickContext(element).Perform
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

07 Dec 2017, 00:02

@malcev
Wow!! it works!! Thank you!! Now I can control Chrome that I want ways, Thank you again!!!!
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

08 Dec 2017, 18:50

If you want to find a specific text on a web page, you can use codes below

Code: Select all

keyword=  ; keyword you want to find

Xpath = //*[text() = '%keyword%']
		
if(driver.FindElementByXPath(Xpath))
	driver.FindElementByXPath(Xpath).click()
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

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()
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!
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

10 Dec 2017, 03:56

@ Joe Glines
Thank you. Your code looks more efficient!

Now I'm searching for ways to get all Xpaths and innterTexts which contains a specific word

Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.Get("http://the-automator.com/web-scraping-with-autohotkey/")

keyword:="website"

if(driver.FindElementByXPath("//*[contains(text(), '" keyword "')]"))
	MsgBox, % driver.FindElementByXPath("//*[contains(text(), '" keyword "')]").Attribute("innerText")
	MsgBox, % driver.FindElementByXPath("//*[contains(text(), '" keyword "')]").item[1].Attribute("innerText")	
First Msgbox code line returns only a value which first founded and the 2nd has an error. I'd like to get 2nd, 3rd, and more value which contains the text. Anybody has any ideas?
Last edited by CH HAN on 11 Dec 2017, 00:22, edited 3 times in total.
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

10 Dec 2017, 04:00

A code below can help you to use Ctrl+A

Code: Select all

driver.FindElementByXPath(Xpath).sendKeys(driver.Keys.CONTROL, "a") ; Ctrl+A

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 32 guests