Page 5 of 9

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 11 Oct 2018, 15:36
by Xtra
SeleniumBasic is a Visual Basic selenium wrapper program providing the COM interface method that AHK uses to control it.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 29 Oct 2018, 21:08
by jeeswg
- I wrote a script to get the webpage titles/urls in Chrome v69.
- Some issues: it doesn't know how many tabs there are, the order is unspecified, it activates each tab (although very quickly).
- If anyone can improve this, I would be grateful.

Code: Select all

;[Google Chrome + SeleniumBasic installation instructions]
;Using Selenium with AutoHotkey- Cross browser automation! - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=7&t=32323&p=151996#p151996

w:: ;chrome + selenium basic - open urls
Loop, 3
{
	Run, % "chrome.exe --remote-debugging-port=9222 https://en.wikipedia.org/wiki/" Chr(64+A_Index)
	Sleep, 800
}
return

q:: ;chrome + selenium basic - get titles/urls
vOutput := ""
oDriver := ChromeGet()

Loop, 3
{
	vOutput .= oDriver.Window.Title "`r`n" oDriver.Url "`r`n`r`n"
	oDriver.SwitchToNextWindow ;change tab
}

MsgBox, % Clipboard := RTrim(vOutput, "`r`n") "`r`n"
return

;[ChromeGet function]
;note: start Chrome with: chrome.exe --remote-debugging-port=9222
;Using Selenium with AutoHotkey- Cross browser automation! - Page 2 - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=7&t=32323&p=181299#p181299

;slight modification by jeeswg of ChromeGet() by tmplinshi
ChromeGet(IP_Port:="127.0.0.1:9222")
{
	local
	driver := ComObjCreate("Selenium.ChromeDriver")
	driver.SetCapability("debuggerAddress", IP_Port)
	driver.Start()
	return driver
}
- Links:
[post by CH HAN and response by malcev]
Using Selenium with AutoHotkey- Cross browser automation! - Page 4 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 49#p231849
[get tab count and other tab info]
Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26947

- Btw is anyone having any success with Firefox and SeleniumBasic? It says here that people have to downgrade to Firefox v46.0.1 to use it with SeleniumBasic.
FireFox, Chrome, Edge, Opera Drivers are not working · Issue #133 · florentbr/SeleniumBasic · GitHub
https://github.com/florentbr/SeleniumBasic/issues/133
- Also, it looks like there has been no activity since Mar 2, 2016.
Release SeleniumBasic v2.0.9.0 · florentbr/SeleniumBasic · GitHub
https://github.com/florentbr/SeleniumBa ... g/v2.0.9.0

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 30 Oct 2018, 23:57
by malcev
it doesn't know how many tabs there are
You can do it like this:

Code: Select all

loop % oDriver.Windows.Count
      msgbox % oDriver.Windows.Item(A_Index).Title "`n" oDriver.Url
it activates each tab
A I know it is impossible to get info from tab without activating it using selenium.
the order is unspecified
The order does not depend on position order, but depends from switch order.
For example if You switch 1-2-3, tab index of number 3 will be 1 and number 1 will be 3.
As a workaround You can compare each tab position through iaccessible interface.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 12 Nov 2018, 17:44
by shipaddicted
I just started messing with this Selenium stuff last night. So far, it works great. I'm hoping I'll be allowed to use it on my work computer to automate a bunch of the useless crap we have to go through to complete the simplest of tasks. Anyhow, I'm trying to mash up a couple of scripts from earlier in this thread and I'm having a little trouble getting what I want. When I run this, I want it to first look and see if I already have Chrome open, and if so, use the existing window. If Chrome is not currently running, run it and go to the site I need. This is what I have:

Code: Select all

#!2::
If WinNotExist, ahk_exe chrome.exe {
	Run,chrome.exe --remote-debugging-port=9222
	}
	Else {
	WinActivate, ahk_exe chrome.exe
	}
driver := ComObjCreate("Selenium.ChromeDriver")
driver.SetCapability("debuggerAddress", "127.0.0.1:9222")
driver.Get("https://www.bing.com")
MsgBox, % driver.Title
Return


If I already have Chrome running, it works okay -- it goes to my most recently tab and opens the new url there (which is not really ideal -- I'd rather open a new tab in most cases, but I'll deal with that part later). But if I don't have Chrome running yet, nothing happens. If I manually open Chrome after trying to run the script, the script will pick up at that point and hijack a tab.

I have tried every version of the if/else statement above I can think of -- If !WinExist, switching the order of the statements, only having one of the statements (If but no else).

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 12 Nov 2018, 17:47
by shipaddicted
Immediately after posting I thought of another thing to try -- IfWinNotExist -- which worked. I wasn't going to bother with it since the documentation said it's deprecated, but Win[Not]Exist() didn't work either, so I thought, why not?

So now I at least have this functioning pretty well:

Code: Select all

#!2::
IfWinNotExist, ahk_exe chrome.exe 
	{
	Run,chrome.exe --remote-debugging-port=9222
	}
	Else 
	{
	WinActivate, ahk_exe chrome.exe
	}
driver := ComObjCreate("Selenium.ChromeDriver")
driver.SetCapability("debuggerAddress", "127.0.0.1:9222")
driver.AddArgument("--user-data-dir=C:\Users\Paige\AppData\Local\Google\Chrome\User Data\Default")    ; Use local chrome profile
driver.AddArgument("disable-infobars") ;disable notification "chrome is being controlled by automated test software"
driver.SetProfile("C:\Users\Paige\AppData\Local\Google\Chrome\User Data\Default")  ;set profile path
driver.Get("https://www.bing.com")
MsgBox, % driver.Title
Return

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 18 Nov 2018, 17:37
by hotkeyguy
Hello,

there is a promising new toy, especially for (but not only) Firefox users (like me):
intoli/remote-browser: A low-level browser automation framework built on top of the Web Extensions API standard.
And it can connect to an already running browser!

No further infos yet, I'm too excited! :superhappy:


Greetings
hotkeyguy

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 21 Nov 2018, 06:53
by adegard
Hi @Joe Glines

Thanks you for all this stuff you give on threads and website...
I create some functions to simplify IE automantion: https://autohotkey.com/boards/viewtopic ... 27#p249027
Could be usefull for someone here

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 02 Dec 2018, 14:35
by Ceruleancerise
Hi,

I've been following along with Joe Glines' tutorial and have installed SeleniumBasic and chromedriver under Joe's direction and have them under /Program Files/.
However, when I try to run a test script to launch chrome (See: https://p.ahkscript.org/?p=c9351c88) I get an error on line 4 that states "The system cannot find the file specified"

Why is this? Have I missed something? Thanks!

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 02 Dec 2018, 18:42
by Xtra
Ceruleancerise wrote:
02 Dec 2018, 14:35
Why is this? Have I missed something? Thanks!

Code: Select all

#SingleInstance, Force
Del::

driver := ComObjCreate("Selenium.ChromeDriver")           ; Start with Chrome
driver.Get("http://duckduckgo.com/")                      ; Navigate to a webpage

MsgBox here
return

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 02 Dec 2018, 22:12
by Ceruleancerise

Code: Select all

#SingleInstance, Force
Del::

driver := ComObjCreate("Selenium.ChromeDriver")           ; Start with Chrome
driver.Get("http://duckduckgo.com/")                      ; Navigate to a webpage

MsgBox here
return
same result :/

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 23 Dec 2018, 03:35
by Sabestian Caine
Joe Glines wrote:
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()
Hello dear Joe Glines.

Sir, I am trying to automate chrome by selenium and i want to get the URL from a button or link in a webpage. I am unable to retrieve the URL from a specific button or link in a webpage. For this i have asked a question in AutoHotKey help form. The link of my question is this-

https://autohotkey.com/boards/viewtopic ... 76&t=60232


I request you to please solve this issue. Thanks a lot sir... :D :D

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 23 Dec 2018, 06:36
by Sabestian Caine
Joe Glines wrote:
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()
Problem solved sir!!!!

this is working-

Code: Select all

MsgBox % driver.findElementsByClass("forumtitle").item[1].Attribute("href") 
Thanks a lot sir..

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 07 Apr 2019, 09:07
by inseption86
Please help...

Code: Select all

driver := ComObjCreate("Selenium.ChromeDriver")
driver.AddArgument("--disable-infobars")
driver.AddArgument("--start-maximized") 

driver.Get("https://www.google.com")

driver.ExecuteScript("window.open();")
driver.SwitchToNextWindow
driver.Get("https://yandex.ru")


w::

;"https://www.google.com"

 MsgBox %  driver.findElementsByTag("input").item[2].Attribute("value")
 return
 
q::

; "https://yandex.ru"
   MsgBox % driver.findElementsByTag("input").item[2].Attribute("value")
 return

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 May 2019, 03:57
by flatwater
How do I click elements inside an iframe?

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 May 2019, 12:45
by Xtra
flatwater wrote:
08 May 2019, 03:57
How do I click elements inside an iframe?
use:

Code: Select all

driver.SwitchToFrame("iframeNameHere") 
And then click the element like normal.

The trick is to always start at the base document and switch to the next frame and then the next (if needed) you can not jump over iframes/frames.
Imagine a tree that you have to go from the trunk and up every branch to your destination but you cant jump across branches,

When you want to go back to the base document use:

Code: Select all

driver.SwitchToDefaultContent()
Overall its very simple compared to IE.

HTH

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 08 May 2019, 19:09
by flatwater
Xtra wrote:
08 May 2019, 12:45
flatwater wrote:
08 May 2019, 03:57
How do I click elements inside an iframe?
use:

Code: Select all

driver.SwitchToFrame("iframeNameHere") 
And then click the element like normal.

The trick is to always start at the base document and switch to the next frame and then the next (if needed) you can not jump over iframes/frames.
Imagine a tree that you have to go from the trunk and up every branch to your destination but you cant jump across branches,

When you want to go back to the base document use:

Code: Select all

driver.SwitchToDefaultContent()
Overall its very simple compared to IE.

HTH
Thank you very much. The iframe doesn't have a name but it is the only iframe in the page. I use driver.SwitchToFrame(0) to switch to the frame and it works. Thanks.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 11 Jul 2019, 02:29
by warwickc
Hi

Loving Selenium, know I've learnt xPath it's really powerful. Joe, your tutorials have been fantastic help over a very long night!

Couple of questions
How do you handle Pop-Up Alerts. I've seen this:

Code: Select all

driver.switchTo().alert().accept();
but think thats java as it doesnt work in AHK, and I cant find the corresponding command to use in AHK.

Also, can you set an implicit wait. Again, according to https www.guru99.com /implicit-explicit-waits-selenium.html Broken Link for safety it should look like

Code: Select all

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
Any help much appreciated - my project is nearly there but it keeps timing out, and I cant deal with those pop ups!
W

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 11 Jul 2019, 07:40
by malcev
Just search Seleniumbasic manual for your commands.

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 12 Jul 2019, 16:56
by mikea105
I normally use an explicit wait. The syntax below seems to work fine with Autohotkey and Selenium. This line waits up to 30 seconds for the element to appear and be clickable. Hopefully it will work for you.

myDynamicElement := (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(driver.executeScript("document.getElementById('ScanButton')")))

Re: Using Selenium with AutoHotkey- Cross browser automation!

Posted: 24 Sep 2019, 15:53
by AHK_user
I was trying to open a new tab, but the popup blocker blocks it.
I tried adding the disable-popup-blocking, but without succes.
Does anybody knows how to disable the popup blocking?


driver.AddArgument("--disable-popup-blocking")
driver.ExecuteScript("window.open();")