Using Selenium with AutoHotkey- Cross browser automation!

Helpful script writing tricks and HowTo's
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

18 Jun 2020, 15:04

@burque505
Looks like you've got this Chrome Selenium thing figured out! Do you by any chance have a fix for the "Chrome is being controlled by automated test software" popup that shows up everytime i use chrome to start selenium on the newest chrome versions?

Code: Select all

driver.AddArgument("disable-infobars") ; Close Message that 'Chrome is being controlled by automated test software'
Works fine on Chrome 72, which i'm still using, but it doesn't work on the newest chrome version. This is the only thing that's holding me back from updating.
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

18 Jun 2020, 15:27

Hi, @Thoughtfu1Tux, I'm running Chrome 83 at the moment. I'm either not getting that "Chrome is being controlled by automated test software" popup at all or it's going by too fast to see.
There's lots of stuff going on here I haven't even scratched the surface of yet. Check this out:

Code: Select all

#Include SChrome_Get.ahk
driver := SChrome_Get("https://www.startpage.com/")
driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
driver.FindElementById("q").ScrollIntoView()
;driver.Window.Maximize	; Maximize the window, not really necessary as chrome starts that way by default
element := SChrome_GetElement(Driver, "q")
SChrome_SendKey(element, "AutoHotkey")
SChrome_SendKey(Driver, "Enter")
ExitApp
Regards,
burque505
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

26 Jun 2020, 18:52

@burque505
Awesome stuff! Thank you for sharing.
I spent a couple hours playing around with your SChrome_Get function and i'm really liking it! I'm working on transferring my selenium scripts to use it now. If you have any other sample scripts that you haven't shared in this thread before, I'd love to see them! :D
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

27 Jun 2020, 07:17

Hi @Thoughtfu1Tux, here's the link for SChromeGet for others - it isn't my script by any means. I'm glad you're having good luck with it.
Regards,
burque505
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

27 Jun 2020, 10:54

Very interesting, @malcev, thank you.
Regards,
burque505
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

21 Oct 2020, 22:49

Anyone know how to send unicode characters like emojis through ChromeDriver?
I want to send the 🔴🔴🔴🔴 Symbol (https://apps.timwhitlock.info/unicode/inspect?s=%F0%9F%94%B4), but the script errors out and says
"ChromeDriver only supports characters in the BMP"

My Code:

Code: Select all

FileEncoding, UTF-8-RAW ; Needed for special symbols

FileRead, Var, specialSymbol.txt    ; file contains the "🔴" emoji
; Msgbox % "Var: " Var
driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
;driver:= ComObjCreate("Selenium.phantomjsDriver") ;Select and Create Chrome driver instance
driver.setbinary("C:\Software\Chromium\chrome.exe") ;Sets path to custom Binary
driver.AddArgument("disable-infobars") ;Hides 'Chrome is being controlled by automated test software' message
driver.AddArgument("--start-maximized") ; Maximize Chrome Browser
driver.AddArgument("--disable-gpu")
;driver.AddArgument("--headless")
driver.Get("https://www.google.com/") ;Open selected URL
driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
driver.findElementsByName("q").item[1].SendKeys(VAR)    ; This is where the script errors out.
msgbox 

Image
malcev
Posts: 802
Joined: 12 Aug 2014, 12:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

22 Oct 2020, 03:55

Use driver.ExecuteScript...
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

22 Oct 2020, 07:11

Haven't got it to work with a variable yet, but this works for me.

Code: Select all

FileEncoding, UTF-8-RAW ; Needed for special symbols
;FileRead, Var, specialSymbol.txt    ; file contains the "🔴" emoji
sleep 100
js =
(
document.getElementsByClassName('gLFyf gsfi')[0].value = "🔴";
)
driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
;driver:= ComObjCreate("Selenium.phantomjsDriver") ;Select and Create Chrome driver instance
;driver.setbinary("C:\Software\Chromium\chrome.exe") ;Sets path to custom Binary
driver.AddArgument("disable-infobars") ;Hides 'Chrome is being controlled by automated test software' message
driver.AddArgument("--start-maximized") ; Maximize Chrome Browser
driver.AddArgument("--disable-gpu")
;driver.AddArgument("--headless")
driver.Get("https://www.google.com/") ;Open selected URL
driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
driver.executeScript(js)   ; This is where the script errors out.
sleep, 1000
Send, {Enter}
msgbox stop
Regards,
burque505
malcev
Posts: 802
Joined: 12 Aug 2014, 12:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

22 Oct 2020, 07:18

I mean like this

Code: Select all

driver.ExecuteScript("arguments[0].value=""\uD83D\uDD34"";", element)
In Your example

Code: Select all

driver.ExecuteScript("arguments[0].value=""\uD83D\uDD34"";", driver.findElementsByName("q").item[1])
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

24 Oct 2020, 13:24

@burque505
@malcev


Both of these worked for me! Thank you very much.
Any idea how to pass a variable to it?
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

24 Oct 2020, 13:46

I figured out the passing a variable bit:

Code: Select all

FileEncoding, UTF-8-RAW ; Needed for special symbols
FileRead, Var, C:\Software\Syncthing\AutoHotKey\testing\description.txt  

; File Contents are 2 lines: 
; 🔴 Bitchute: bitchute.com/
; 🔴 LBRY: lbry.tv

; Msgbox % "Var: " Var
sleep 100
js =
(
document.getElementsByClassName('textarea -form js-paste-code')[0].value = "%Var%";
)
Msgbox % "js: " js

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
; driver.setbinary("C:\Software\Chromium\chrome.exe") ;Sets path to custom Binary
driver.Get("https://pastebin.com/") ;Open selected URL
driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
driver.executeScript(js) 
sleep, 1000
Send, {Enter}
msgbox stop

This works just fine if the text file has only 1 line. But if it has 2 lines the script errors out.
I'm thinking I need to replace the newline character with something javascripty.

Image
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

24 Oct 2020, 14:01

Figured it out! Needed to replace autohotkey's newline strings with the HTML/universal ones.

Code: Select all


FileEncoding, UTF-8-RAW ; Needed for special symbols
FileRead, Var, C:\Software\Syncthing\AutoHotKey\testing\description.txt   ; file contains the "🔴" emoji

; File Contents are 2 lines: 
; 🔴 Bitchute: bitchute.com/
; 🔴 LBRY: lbry.tv

; Replace `n (newline) with nothing and then `r (return) with a "\n" (Universal new line character?)
Var := StrReplace(Var, "`n", "")
Var := StrReplace(Var, "`r", "\n")
Msgbox % "var: " var	; result is: 🔴 Bitchute: bitchute.com/ /n 🔴 LBRY: lbry.tv


sleep 100
js =
(
document.getElementsByClassName('textarea -form js-paste-code')[0].value = "%Var%";
)

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
driver.setbinary("C:\Software\Chromium\chrome.exe") ;Sets path to custom Binary
driver.Get("https://pastebin.com/") ;Open selected URL
driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
driver.executeScript(js)   ; This is where the script errors out.
sleep, 1000
Send, {Enter}
msgbox stop


burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

24 Oct 2020, 16:42

@Thoughtfu1Tux, thanks for that, and thanks for keeping at it. As per usual, I was not quoting the variable correctly in my continuation section.
As you've no doubt discovered, on both Win10 and Win7 the line

Code: Select all

FileEncoding, UTF-8-RAW ; Needed for special symbols
is not needed.
Regards,
burque505
malcev
Posts: 802
Joined: 12 Aug 2014, 12:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

25 Oct 2020, 02:41

1) You dont need driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
2) If You use selenium why dont You want to use selenium methods for DOM, like this?

Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
driver.Get("https://pastebin.com/") ;Open selected URL
driver.ExecuteScript("arguments[0].value=""" var """;", driver.FindElementByClass("textarea"))
User avatar
tadamm
Posts: 21
Joined: 27 Sep 2020, 11:41

Re: Using Selenium with AutoHotkey- Cross browser automation!

31 Oct 2020, 21:22


[Mod edit: link fixed]

Much simpler way for getting AHK to talk with chrome. Simple AHK code and Simple JS code using an ext,
No Selenium, no debug mode, no web connectors
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

01 Nov 2020, 09:19

@tadamm, nice video. Since you won't be using Selenium (I take it), it might be worth continuing this in a new topic. Looking forward to more videos.
Regards,
burque505
User avatar
tadamm
Posts: 21
Joined: 27 Sep 2020, 11:41

Re: Using Selenium with AutoHotkey- Cross browser automation!

01 Nov 2020, 14:24

burque505 wrote:
01 Nov 2020, 09:19
@tadamm, nice video. Since you won't be using Selenium (I take it), it might be worth continuing this in a new topic. Looking forward to more videos.

TY i toke your advice and did that here

https://www.autohotkey.com/boards/viewtopic.php?f=74&t=82730
User avatar
Thoughtfu1Tux
Posts: 109
Joined: 31 May 2018, 23:26

Re: Using Selenium with AutoHotkey- Cross browser automation!

07 Nov 2020, 17:06

@burque505
Do you know of away to make schrome_get connect to a second chrome window? Like open up a new window/instance of chrome and connect to that window instead of the first window? They would both be using the same profile.
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: Using Selenium with AutoHotkey- Cross browser automation!

07 Nov 2020, 17:44

hi @Thoughtfu1Tux, is something like this an option for you? (Quick and dirty. There's a better way to get rid of the Chrome pages.)

Code: Select all

 #Include SChrome_get.ahk
 
driver := SChrome_Get("http://www.rpachallenge.com")
driver.ExecuteScript("document.querySelector('button.waves-effect').click();")
sleep 2000

putter := SChrome_Get("https://www.autohotkey.com")
sleep 2000

msgbox Eagle on hole 18!!!
WinKill, ahk_exe Chrome.exe
WinKill, ahk_exe Chrome.exe
Regards,
burque505

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 6 guests