Using Selenium with AutoHotkey- Cross browser automation!

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

Re: Using Selenium with AutoHotkey- Cross browser automation!

19 Sep 2017, 23:41

You could very easily set the user & pass elements and click the login button.
Logging into a site is no different than automating anything else on a website.
By doing this you wont have to rely on cookies which will eventually expire. Just a thought.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

20 Sep 2017, 07:19

Try this:
After the Selenium version of Chrome loads, use the same process you did for your regular chrome executable to get the path of the current profile (put "chrome://version/" in the url). Take a look at the "Profile Path" that it is using. Is it what you've defined in your script?

If it is, use explorer to navigate to that folder and verify you have a "cookies" file. When I opened mine I can find LinkedIn.com (which is what i'd logged into for testing of this)
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!

24 Sep 2017, 23:09

Joe Glines / It works! I copied and used profile path after had created Chrome window by Selenium. The profile paths of Chrome opened by manually and Selenium are different. Thank you!

Xtra / Your code also works! Thank you!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

25 Sep 2017, 07:49

:) That's great!
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!

28 Oct 2017, 19:09

seleniumError.jpg
seleniumError.jpg (567.72 KiB) Viewed 12050 times
Hi, I've tried to login a webpage but I couldn't. I think it's because of the class name of a blank which I want to put in my login ID. it has spaces between letters of it. The class name is 'ng-pristine ng-invalid ng-touched' And my code is like this

Code: Select all

driver.executeScript("arguments[0].setAttribute('value', 'abcd')", driver.findElementsByClass("ng-pristine ng-invalid ng-touched"))
AHK pops up an error message that InvalidSelectorError, I attached a screen shot file of that. I don't know how I can handle it.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

28 Oct 2017, 19:38

You can always use Xpath or CSS

Xpath working Example (minus real creds edit to your own)

Code: Select all

driver.FindElementByXPath("/html/body/fg-root/div[1]/fg-public-layout/fg-auth/div[1]/div/div/div[1]/div/div/form/div[1]/input").SendKeys("abcd")    ; Username
driver.FindElementByXPath("/html/body/fg-root/div[1]/fg-public-layout/fg-auth/div[1]/div/div/div[1]/div/div/form/div[2]/input").SendKeys("1234")    ; Password
driver.FindElementByXPath("/html/body/fg-root/div[1]/fg-public-layout/fg-auth/div[1]/div/div/div[1]/div/div/form/div[4]/button").click()            ; Button
HTH
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

29 Oct 2017, 22:15

Gosh, it works. I didn't know about Xpath. Thank you!!!! :bravo:
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

29 Oct 2017, 22:34

Hi,

Anybody knows how to keep browser open after end of calling function? I have made some codes using Selenium in a function, and I call it from main.ahk file. I want to keep the browser which I use in the function after end of calling the function but it closes automatically when the function is closed. I've googled about this and many people suggested that using sleep(900000), but I can't use that because I want to process other task while the used browser opened. Anybody has any solution about it? And can i reuse the browser in other function? Thank you.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Using Selenium with AutoHotkey- Cross browser automation!

30 Oct 2017, 01:33

Put this at top of the script: (Autoexec section)

Code: Select all

Global driver
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

31 Oct 2017, 00:46

It's simple. I'll try that. Thank you. But I still don't know the way to leave a browser opened after end of scripts. I should know that first to reuse the browser. :crazy:
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
}

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 28 guests