Nice Work, tmplinshi I was just looking for a way to detect debugmode some time ago
.
I combined the functions to have one function (To rule them all)
SChrome_Get() to connect to Chrome.
If Chrome was already running in normal mode, it asks if it may restart Chrome.
I am impressed by the speed of selenium, I have a protocol on my work where I webscrape a Web-based Product Data Management (PDM) system(Windchill).
A specific protocol timed 9.4 seconds in IE and 3.5 seconds in Chrome with Selenium, so a nice improvement.
With Xpath it is easy to get elements.
For those who have trouble with Xpath, try to use
SChrome_GetElement() to generate the Xpath or just use the function.
Code: Select all
; Written by AHK_User 2019-10-11
; Special thanks to tmplinshi, CH HAN and Joe Glines
SChrome_Get(URL := "", IP_Port := "127.0.0.1:9222"){
IP_Port_Nr := RegExReplace(IP_Port, ".*:(\d*)", "$1")
if WinExist("ahk_exe Chrome.exe"){
WinGet, pid, PID, ahk_exe chrome.exe
for item in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId='" pid "'"){
if RegExMatch(item.CommandLine, "i)--remote-debugging-port=\K\d+", port){
break
}
}
if (Port=""){
MsgBox, 36, , Is it ok to restart Chrome in debugmode to enable a connection?
IfMsgBox, Yes
{
While(WinExist("ahk_exe chrome.exe")) {
WinClose, ahk_exe chrome.exe
}
Process, WaitClose, chrome.exe
}
Else{
Exit
}
}
}
if(URL!="" or !winExist("ahk_class Chrome_WidgetWin_1")){
run % "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --remote-debugging-port=" IP_Port_Nr ( winExist("ahk_class Chrome_WidgetWin_1") ? " --new-window " : " " ) URL
}
Driver := ComObjCreate("Selenium.ChromeDriver")
Driver.SetCapability("debuggerAddress", IP_Port)
Driver.Start()
return Driver
}
SChrome_GetElement(Driver, Value, Type:="id", Tag:="*", Search:="0"){
;https www.guru99.com /xpath-selenium.html Broken Link for safety
if (Tag!=""){
xPath := "//" Format("{:L}", Tag)
}
else {
xPath := "//*"
}
if (Search=0){
xPath .= "["
}
else {
xPath .= "[contains("
}
if (Type="text"){
xPath .= "text()"
}
else{
; type: id, name, class,value, href, type,
xPath .= "@" Format("{:L}", Type)
}
if (Search=0){
xPath .= "=""" Value """]"
}
else {
xPath .= ",""" Value """)]"
}
;~ MsgBox, % xPath
return Driver.findelementbyxpath(xPath)
}
SChrome_SendKey(DriverOrElement, Key){
;https artoftesting.com /automationTesting/press-enter-tab-space-arrow-function-keys-in-selenium-webdriver-with-java.html Broken Link for safety
if (Key="Enter"){
Key := DriverOrElement.Keys.ENTER
}
else if (Key="Escape"){
Key := DriverOrElement.Keys.ESCAPE
}
DriverOrElement.SendKeys(Key)
return
}
SChrome_SetElementValue(Driver, Element, Value, Attibute:="value"){
Driver.executeScript("arguments[0].setAttribute('" Attibute "', '" Value "')", Element)
return
}
SChrome_RightClick(Driver, Element){
Driver.Actions.ClickContext(Element).Perform
return
}
SChrome_Wait(Driver){
; Wait till website is loaded
Driver.executeScript("return document.readyState").equals("complete") ; wait until page loads
return
}
SChrome_UpdateDriver(){
; Function Updates the Chromedriver by checking the versions and downloading the latest chromedriver.
FileGetVersion, Version_Chrome, C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Version_Chrome := RegexReplace(Version_Chrome, "(.*)\.\d\d", "$1")
; Get Chromedriver version
Version_ChromeDriver := RunHide("""C:\Users\" A_UserName "\AppData\Local\SeleniumBasic\chromedriver.exe"" --version")
Version_ChromeDriver := RegexReplace(Version_ChromeDriver, "[^\d]*([\.\d]*).*", "$1")
;~ DebugWindow("Version Chrome:" Version_Chrome "`nVersion Chromedriver:" Version_Chromedriver,Clear:=0,LineBreak:=1)
; Check if versions are equal
if InStr(Version_Chromedriver, Version_Chrome){
MsgBox,68,Testing,Current Chromedriver is same as Chromeversion.`nDo you still want to download?
IfMsgBox, No
{
exit
}
}
; Find the matching Chromedriver
oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_" Broken Link for safety Version_Chrome, true)
oHTTP.Send()
oHTTP.WaitForResponse()
Version_Chromedriver := oHTTP.ResponseText
;~ DebugWindow("The latest release of Chromedriver is:" Version_ChromeDriver,Clear:=0,LineBreak:=1)
if InStr(Version_Chromedriver, "NoSuchKey"){
MsgBox,16,Testing,Error`nVersion_Chromedriver
return
}
; Download the Chromedriver
Url_ChromeDriver := "https chromedriver.storage.googleapis.com /" Broken Link for safety Version_Chromedriver "/chromedriver_win32.zip"
URLDownloadToFile, %Url_ChromeDriver%, %A_ScriptDir%/chromedriver_win32.zip
; Unzip Chromedriver_win32.zip
fso := ComObjCreate("Scripting.FileSystemObject")
AppObj := ComObjCreate("Shell.Application")
FolderObj := AppObj.Namespace(A_ScriptDir "\chromedriver_win32.zip")
FileObj := FolderObj.ParseName("chromedriver.exe")
AppObj.Namespace(A_ScriptDir "\").CopyHere(FileObj, 4|16)
return
}
RunHide(Command) {
Run, %ComSpec% /K,, Hide UseErrorLevel, cPid
Process, Wait, %cPid%
DllCall("AttachConsole", "uint", cPid)
Shell := ComObjCreate("WScript.Shell")
Exec := Shell.Exec(Command)
Result := Exec.StdOut.ReadAll()
DllCall("FreeConsole")
Process, Close, %cPid%
Return Result
}