Page 3 of 7

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 01 Dec 2017, 11:22
by Joe Glines
I'm curious if this will affect Selnium & ahk use
Google will lock down chrome on Windows

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 02 Dec 2017, 18:33
by CH HAN
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

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 02 Dec 2017, 18:48
by CH HAN
R_google_drive.jpg
R_google_drive.jpg (78.09 KiB) Viewed 7473 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~?!?!

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 02 Dec 2017, 19:57
by Joe Glines
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)

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 03 Dec 2017, 19:26
by malcev

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

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 07 Dec 2017, 00:02
by CH HAN
@malcev
Wow!! it works!! Thank you!! Now I can control Chrome that I want ways, Thank you again!!!!

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 Dec 2017, 18:50
by CH HAN
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()

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 Dec 2017, 19:48
by Joe Glines
@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()

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 10 Dec 2017, 03:56
by CH HAN
@ 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?

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 10 Dec 2017, 04:00
by CH HAN
A code below can help you to use Ctrl+A

Code: Select all

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

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 16 Dec 2017, 01:42
by CH HAN
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")
}

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 16 Dec 2017, 01:48
by CH HAN
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

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 16 Dec 2017, 07:25
by Joe Glines
@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...

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 14 Jan 2018, 04:10
by CH HAN
@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")

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 28 Jan 2018, 01:12
by CH HAN
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.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 16 Feb 2018, 07:02
by Joe Glines
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.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 Jun 2018, 10:47
by flatwater
Thank you for the tutorials.

How do I hide the chrome window? Thanks.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 Jun 2018, 15:41
by Joe Glines
That you can probably do with just a standard WinMinimize command https://autohotkey.com/docs/commands/WinMinimize.htm

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 09 Jun 2018, 01:24
by tmplinshi
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

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 09 Jun 2018, 22:49
by flatwater
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.