Using Selenium with AutoHotkey- Cross browser automation!

Helpful script writing tricks and HowTo's
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
GitHub: MagnaPax

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
GitHub: MagnaPax

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 3178 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: 563
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
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)
malcev
Posts: 112
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
GitHub: MagnaPax

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
GitHub: MagnaPax

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: 563
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
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()
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
GitHub: MagnaPax

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
GitHub: MagnaPax

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
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
GitHub: MagnaPax

Re: Using Selenium with AutoHotkey- Cross browser automation!

16 Dec 2017, 01:42

I've found the answer of the question I posted above, it's using brackets. If you want use elements sharing same Xpath, you should add brackets end of Xpath like (Xpath)[2] or (Xpath)[3]

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 "')])[2]").Attribute("innerText")
}
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
GitHub: MagnaPax

Re: Using Selenium with AutoHotkey- Cross browser automation!

16 Dec 2017, 01:48

You should check this code to wait for a web page is loaded.

Code: Select all

driver.executeScript("return document.readyState").equals("complete") ; wait until page loads
User avatar
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

16 Dec 2017, 07:25

@CH HAN - Sorry I didn't see your previous question about accessing the other words. yes, adding brackets will access the items as an array. If I remember correctly, in Selenium, they are not zero based (unlike in COM where they are).

Regarding the page loading- Have you had issues with Selenium moving forward before the page fully loaded? In my experimenting I didn't have that issue...
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
GitHub: MagnaPax

Re: Using Selenium with AutoHotkey- Cross browser automation!

14 Jan 2018, 04:10

@Joe Glines
Sorry for the late reply, I've not touched Selenium for long time. The answer is Yes. Sometimes Selenium just continues codes even while browser is still loading a page so I change the waiting code like the below but it still not works perfectly either.

Code: Select all

driver.executeScript("return document.readyState").toString().equals("complete")
CH HAN
Posts: 31
Joined: 18 Sep 2017, 02:16
GitHub: MagnaPax

Re: Using Selenium with AutoHotkey- Cross browser automation!

28 Jan 2018, 01:12

Set defaults of Chrome browser

Code: Select all

driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
driver.AddArgument("disable-infobars") ; Close Message that 'Chrome is being controlled by automated test software'
driver.AddArgument("--start-maximized") ; Maximize Chrome Browser

driver.Get("http://duckduckgo.com/")

driver.ExecuteScript("document.body.style.zoom = '100%';") ; Set the font of Chrome browser to 100%
However, these are not working for Chrome browsers controlled by ChromeGet() function.
User avatar
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Using Selenium with AutoHotkey- Cross browser automation!

16 Feb 2018, 07:02

Just curious if anyone on this thread has tried GeekDude's solution for automating Chrome w/o Selenium. I worked on some functions to use with it for setting/getting data but haven't had time to finish them up and share.
flatwater
Posts: 5
Joined: 28 Dec 2017, 08:21

Re: Using Selenium with AutoHotkey- Cross browser automation!

08 Jun 2018, 10:47

Thank you for the tutorials.

How do I hide the chrome window? Thanks.
tmplinshi
Posts: 1281
Joined: 01 Oct 2013, 14:57

Re: Using Selenium with AutoHotkey- Cross browser automation!

09 Jun 2018, 01:24

flatwater wrote:How do I hide the chrome window?
http://chromedriver.chromium.org/capabilities
Tried with driver.SetCapability("args", "['headless']") but didn't work. Anyway, below are two ways that worked:

Method 1: Using --headless

Code: Select all

Run, chrome.exe --remote-debugging-port=9222 --headless --disable-gpu --disable-extensions,,, pid

driver := ComObjCreate("Selenium.ChromeDriver")
driver.SetCapability("debuggerAddress", "127.0.0.1:9222")
driver.Get("https://www.bing.com")
MsgBox, % driver.Title

Process, Close, %pid%
Method 2: Using PhantomJS

Code: Select all

driver := ComObjCreate("Selenium.PhantomJSDriver")
driver.Get("https://www.bing.com")
MsgBox, % driver.Title
flatwater
Posts: 5
Joined: 28 Dec 2017, 08:21

Re: Using Selenium with AutoHotkey- Cross browser automation!

09 Jun 2018, 22:49

tmplinshi wrote:
flatwater wrote:How do I hide the chrome window?
http://chromedriver.chromium.org/capabilities
Tried with driver.SetCapability("args", "['headless']") but didn't work. Anyway, below are two ways that worked:

Method 1: Using --headless

Code: Select all

Run, chrome.exe --remote-debugging-port=9222 --headless --disable-gpu --disable-extensions,,, pid

driver := ComObjCreate("Selenium.ChromeDriver")
driver.SetCapability("debuggerAddress", "127.0.0.1:9222")
driver.Get("https://www.bing.com")
MsgBox, % driver.Title

Process, Close, %pid%
Method 2: Using PhantomJS

Code: Select all

driver := ComObjCreate("Selenium.PhantomJSDriver")
driver.Get("https://www.bing.com")
MsgBox, % driver.Title
Thank you very much. I used the first method.

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 6 guests