Page 4 of 31
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 25 Apr 2022, 08:23
by Milchmann
Xeo786 wrote: ↑25 Apr 2022, 08:02
Milchmann wrote: ↑25 Apr 2022, 07:29
Do you have an example. I don`t understand......
paste following url into address bar of chrome and check correct profile path
chrome://version/
Sorry, probably poorly worded.
I need an exact example of how you generated the error message
".error says "cannot create default profile directory" hmmm " with .error.
Thank you..
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 26 Apr 2022, 00:59
by Xeo786
Milchmann wrote: ↑25 Apr 2022, 08:23
Sorry, probably poorly worded.
Sorry for the confusion, I was using phone to reply and failed to read properly
Milchmann wrote: ↑25 Apr 2022, 08:23
I need an exact example of how you generated the error message
".error says "cannot create default profile directory" hmmm " with .error.
its there in newsession()
https://github.com/Xeo786/Rufaydium-Webdriver/blob/main/Rufaydium.ahk#L56
response.error will tell the actual error.
and check out error handling for Errors
https://github.com/Xeo786/Rufaydium-Webdriver#error-handling
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 26 Apr 2022, 02:47
by Milchmann
Top!
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 27 Apr 2022, 07:33
by burque505
@Xeo786, thank you for all your hard work on this project. I'm looking forward to trying it out.
Regards,
burque505
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 27 Apr 2022, 11:40
by thalesduarte
Can you share an example script by filling out a form on the web and clicking the SUBMIT button?
So far I haven't been able to make the function work
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 27 Apr 2022, 12:30
by ahk7
EDIT/COMMENT: the code below is no longer valid with newer versions of Rufaydium, please consult the documentation on github
thalesduarte wrote: ↑27 Apr 2022, 11:40
Can you share an example script by filling out a form on the web and clicking the SUBMIT button?
This visits the ahk forum, searches and then closes the script after the MsgBox, probably as short as it can be as an example. Here I use
.SendKey() to press enter to start the search, but
.click() on the button element in other cases might be more useful.
Code: Select all
#SingleInstance, force
#Include %A_ScriptDir%\lib
#Include Rufaydium.ahk
ChromeDriver := A_ScriptDir "\chromedriver.exe"
url:="https://www.autohotkey.com/boards/"
; choose different driver in order to automate different Browser
Driver := new RunDriver(ChromeDriver) ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
; choosing Browser Capabilities, by using Capabilities you can make custom profile for specific need
Chrome.capabilities := Capabilities.ChromeDefault
; this is how we create session
Page := Chrome.NewSession()
Page.Navigate(url)
GS := Chrome.getSessionByUrl(url)
Element := GS.getElementsbyXpath("//*[@id=""keywords""]")
; or
; element:=GS.getElementbyID("keywords")
element[1].value:="Rufaydium"
element[1].SendKey(key.enter)
MsgBox, Now closing script, chrome and webdriver
GS:=""
Page := ""
Chrome.QuitAllSessions()
driver.exit()
ExitApp
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 28 Apr 2022, 02:35
by Milchmann
Hello, @Xeo786 ,
Is there any way to detect the active TAB from Chrome and then continue to work on it then. I do not know at this time what url or title the active TAB has.
Thanks
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 28 Apr 2022, 03:00
by Xeo786
Milchmann wrote: ↑28 Apr 2022, 02:35
Is there any way to detect the active TAB from Chrome and then continue to work on it then. I do not know at this time what url or title the active TAB has.
Code: Select all
msgbox, % Session.title() ; will show active title
msgbox, % Session.url() ; will show active title
Also updated documents
https://github.com/Xeo786/Rufaydium-Webdriver/blob/main/README.md#webdriver-sessions
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 28 Apr 2022, 06:44
by Milchmann
Bad question again.
I started a session. Then I opened a page via the web driver and performed an action. Now I open a new TAB manually or through the opened page, which is now in the foreground.
I can't get over this new TAB
Code: Select all
Session := Chrome.getSessionByUrl(URL)
Session2 := Chrome.getSessionByTitle(Title)
query because I don't know what title or what url the active window has.
Do you have a solution for this?
Thanks
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 28 Apr 2022, 07:14
by Xeo786
Milchmann wrote: ↑28 Apr 2022, 06:44
Bad question again.
I started a session. Then I opened a page via the web driver and performed an action. Now I open a new TAB manually or through the opened page, which is now in the foreground.
I can't get over this new TAB
Code: Select all
Session := Chrome.getSessionByUrl(URL)
Session2 := Chrome.getSessionByTitle(Title)
query because I don't know what title or what url the active window has.
Do you have a solution for this?
Thanks
You can use
SwitchbyTitle() or
SwitchbyURL()
https://github.com/Xeo786/Rufaydium-Webdriver/blob/main/README.md#switchbytitle--switchbyurl
but in your case you are manually opening tab, but first tab is still active for Rufaydium,
you can use above method in order to active second tab or in case you do not have Title or URL then
You can also get details of all tabs using
Session.GetTabs() it will return an array having Unique WIndow ID, by using that UWID you can switch to specific tab
Code: Select all
TabsDetail := Session.GetTabs()
msgbox, % json.dump(tabsDetail) ; this will show you how this object look like
; suppose there are 2 tabs
msgbox, % TabsDetail[1] ; will show first tabs unique Window ID
msgbox, % TabsDetail[2] ; will show second tabs unique Window ID
; you can switch to specific tab using that unique Window ID
Session.switch(TabsDetail[1]) ; will switch to first tab
; you can now check title and url for first tab
msgbox, % Session.title() "`n" Session.url()
Session.switch(TabsDetail[2]) ; will switch to second tab
; you can now check title and url for second tab
msgbox, % Session.title() "`n" Session.url()
SwitchbyTitle() and
SwitchbyUrl() are depicting above methods
https://github.com/Xeo786/Rufaydium-Webdriver/blob/main/Rufaydium.ahk#L165
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 29 Apr 2022, 02:12
by Xeo786
Milchmann wrote: ↑28 Apr 2022, 06:44
query because I don't know what title or what url the active window has.
This issue is the reason, I have added methods, will help access session / tabs by numbers
getSession(1) https://github.com/Xeo786/Rufaydium-Webdriver#new-session
SwitchTab(1) https://github.com/Xeo786/Rufaydium-Webdriver#switchtab-switchbytitle--switchbyurl
Thank you
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 01 May 2022, 04:13
by aifritz
I've played a bit with session.isLoading()
What I don't unterstand is, why session.isLoading() never gives any value back.
This script is only working, when you set TestWithIsLoading := 0
Code: Select all
#Include %A_ScriptDir%\lib
#Include Rufaydium.ahk
ChromeDriver := A_ScriptDir "\chromedriver.exe"
Return
F4::
Driver := new RunDriver(ChromeDriver) ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
;TestWithIsLoading := 1 ;not working
TestWithIsLoading := 0 ;working
Chrome.capabilities := Capabilities.ChromeProfile
session := Chrome.NewSession()
Session.Maximize()
session.Navigate("https://www.autohotkey.com/")
SplashTextOn, , , Click Forums Button...
If TestWithIsLoading ;not working
{
While session.isLoading()
Sleep 200
session.findelement(by.linktext,"Forums").click()
}
else ;working
{
;Wait until Button Forums is available
MyButton := ""
While !IsObject(MyButton)
{
sleep, 200
MyButton := session.findelement(by.linktext,"Forums")
MyButton.click() ;Click on Forums Button
}
}
SplashTextOff
If TestWithIsLoading ;not working
{
While session.isLoading()
Sleep 200
}
else ;working
{
while !IsObject(TitleObj := Session.getElementsbyClassName("forumtitle")) ;Waiting for Forumssite
sleep, 200
}
Sleep 200
AllTitles := ""
For k, tag in TitleObj
AllTitles .= k "`t" tag.innertext "`n"
If TitleObj.length()
msgbox,4096,, % "There are " TitleObj.length() " Forumtitles`n`n" AllTitles "`n`nNow we click on advanced search looking for Rufaydium..."
else
msgbox,4096,, % "Ups, could not get the Forumtitles!"
SplashTextOn, , , Click Button Advanced Search...
loop
{
myClick := session.findelement(by.linktext,"Advanced search").click()
If myClick.error
break
Sleep 1000
SplashTextOn, , , Click again Button Advanced Search...
}
SplashTextOff
Sleep 200
session.findelement(by.selector,"#keywords").value := "Rufaydium" ;oder session.getelementbyid("keywords")
session.findelement(by.selector,"#page-body > form > div.panel.bg1 > div > fieldset > input.button1").click()
msgbox,4096,, % "Here are the topics for Rufaydium`n`nNow closing all sessions..."
Chrome.QuitAllSessions()
driver.exit()
return
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 02 May 2022, 06:05
by Milchmann
Hi Xeo,
thank you, that helps.
But I still can't see which tab is the currently focused one.
I also found the following on the internet:
https://developer.chrome.com/docs/extensions/reference/tabs/
Get the current tab
This example demonstrates how the background script can retrieve the currently focused tab.
This example requires Manifest V3 due to the use of Promises. Additionally, content scripts cannot use tabs.query.
Code: Select all
//// background.js
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
Bert
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 02 May 2022, 07:46
by Nixcalo
It may be a stupid question, but how do I get Rufaydium to open an Edge session instead of a Chrome one? I need to obtain information from Edge frames.
Code: Select all
#Include Rufaydium.ahk
ChromeDriver := A_ScriptDir "\chromedriver.exe"
; choose different driver in order to automate different Browser
Driver := new RunDriver("ChromeDriver.exe") ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
; choosing Browser Capabilities, by using Capabilities Class you can make custom profile for specific need
; Chrome.capabilities := Capabilities.ChromeDefault
; this is how we create session
Page := Chrome.NewSession()
Page.Navigate("https://www.elmundo.es/")
Page := ""
return
To be precise, I have a script that worked in IE that need migrate to Edge. It's like this.
Code: Select all
IE := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Deepl Translate and retrieve translation
IE.Visible := false
IE.Navigate(Path)
While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy ; Wait until page is loaded?
Sleep 50
While (IE.document.getElementsByTagName("textarea")[1].value = "") ;Since the conversion takes a second we want to wait till the value is filled otherwise the return will always be nothing
{
Sleep 50
If (A_Index >190) { ; Taking too long
; MsgBox ,,,Error`, starting, .5
IE.Quit
Return Result:="Translation failed"
}
}
Result := IE.document.getElementsByTagName("textarea")[1].value; THIS IS THE RESULT I WANT!
This is what I want migrated into MS Edge, ideally, or otherwise Chrome/Firefox.
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 04 May 2022, 08:59
by Milchmann
Hi
@Xeo786
I'm working my way more and more into your good work!
4 points are still open.
1.
Can you still create a function for me?
viewtopic.php?p=460012#p460012
2.
I get at
Code: Select all
msgbox % session.Detail()
msgbox % session.element.Displayed()
msgbox % session.element.enabled()
does not return any values.
3.
Code: Select all
Msgbox % Page.ExecuteSync("return window.devicePixelRatio")
Always brings as value 1 or 01 when I change the zoom in the browser. In the Java Console it brings me the correct results with for example 25% zoom = value "0.25".
4.
works not.
Thanks
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 06 May 2022, 05:39
by Xeo786
Milchmann wrote: ↑02 May 2022, 06:05
But I still can't see which tab is the currently focused one.
Bert
Hope following code would resolve issue
Code: Select all
Driver := new RunDriver(ChromeDriver) ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
Session := Chrome.getSession(1) ; getting session by number as we do not know page title or url
; checking that session title and url
msgbox, % Session.title() "`n" Session.url()
Nixcalo wrote: ↑02 May 2022, 07:46
To be precise, I have a script that worked in IE that need migrate to Edge. It's like this.
Code: Select all
IE := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Deepl Translate and retrieve translation
IE.Visible := false
IE.Navigate(Path)
While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy ; Wait until page is loaded?
Sleep 50
While (IE.document.getElementsByTagName("textarea")[1].value = "") ;Since the conversion takes a second we want to wait till the value is filled otherwise the return will always be nothing
{
Sleep 50
If (A_Index >190) { ; Taking too long
; MsgBox ,,,Error`, starting, .5
IE.Quit
Return Result:="Translation failed"
}
}
Result := IE.document.getElementsByTagName("textarea")[1].value; THIS IS THE RESULT I WANT!
This is what I want migrated into MS Edge, ideally, or otherwise Chrome/Firefox.
if that page is google translate than it would be something like
Code: Select all
msEdgeDriver := A_ScriptDir "\msedgedriver.exe"
Driver := new RunDriver(msEdgeDriver) ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
Page := Chrome.getSessionByUrl("https://translate.google.com") ; get session by url if we have previously created session
if !IsObject(Page)
{
Page := Chrome.NewSession()
Page.Navigate("https://translate.google.com/?hl=en&tab=rT&sl=auto&tl=es&op=translate")
; no need to check ready state as Webdriver basic has builtin await read this https://github.com/Xeo786/Rufaydium-Webdriver#await
}
TextArea := Page.querySelector("textarea")
TextArea.sendkey("xyz")
msgbox, % TextArea.value
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 06 May 2022, 05:56
by Xeo786
I suspect I just answered that in above post, if not please elaborate
Milchmann wrote: ↑04 May 2022, 08:59
2.
I get at
Code: Select all
msgbox % session.Detail()
msgbox % session.element.Displayed()
msgbox % session.element.enabled()
does not return any values.
session.Detail() will return will page detail same as
localhost:9222/json/list
Code: Select all
msgbox % json.dump(session.Detail())
First you need to get element as
.Displayed() and
.enabled() are methods from WDElement class
Code: Select all
Element := Session.querySelector(someselector)
msgbox, % "displayed: " Element.Displayed() "`nEnabled: " Element.enabled()
Milchmann wrote: ↑04 May 2022, 08:59
3.
Code: Select all
Msgbox % Page.ExecuteSync("return window.devicePixelRatio")
Always brings as value 1 or 01 when I change the zoom in the browser. In the Java Console it brings me the correct results with for example 25% zoom = value "0.25".
above code is working for me, you can calculate actual percentage
Code: Select all
msgbox % round(Page.ExecuteSync("return window.devicePixelRatio") * 100,0) "%"
Milchmann wrote: ↑04 May 2022, 08:59
4.
works not.
scrollIntoView() is not valid method
you can send null key to have focus and scroll into view that element
Code: Select all
Element := Session.querySelector(someselector)
Element.sendkey(json.null) ; for input elements
Element.click() ; for non input elements
you can use
Session.actions() using coord from
element.rect() for clickable elements
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 06 May 2022, 13:49
by AHK_user
Great work!
May I propose to include this usefull piece of code of mine that downloads the driver automatically if the driver is not found or if the version is wrong. Why bothering figuring out if what the version of chrome is and where to download the driver?
It does some assumptions about the location of the ChromeDriver.exe
and Chrome.exe, but I think they are acceptable.
EDITED POST: It looks in the registry for the location of Chrome.exe
I would include it in the driver definition, if the first parameter of RunDriver is empty, just apply this...
Code: Select all
#SingleInstance force
ChromeDriver_Update()
return
ChromeDriver_Update(Dir_Chromedriver:=""){
; Function Updates the Chromedriver by checking the versions and downloading the latest chromedriver.
; Written by AHK_User
; Thanks to tmplinshi
Dir_Chromedriver := Dir_Chromedriver="" ? (FileExist(A_ScriptDir "\chromedriver.exe") ? A_ScriptDir "\chromedriver.exe" : "")
Dir_Chromedriver := Dir_Chromedriver="" ? RegexReplace(A_MyDocuments,"^(.*)\\.*","$1") "\AppData\Local\Google\Chrome\chromedriver.exe" : Dir_Chromedriver
SplitPath, Dir_Chromedriver, , Folder_Chromedriver
if FileExist(Dir_Chromedriver){
; Get Chromedriver version
Version_ChromeDriver := RunHide("""" Dir_Chromedriver """ --version")
Version_ChromeDriver := RegexReplace(Version_ChromeDriver, "[^\d]*([\.\d]*).*", "$1")
} else{
Version_ChromeDriver := "Error"
}
RegRead, File_ChromeExe, % "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Chrome.exe"
if (File_ChromeExe=""){
MsgBox, Failed to find the chrome.exe directory`nCurrent thread will exit.
Exit
}
FileGetVersion, Version_Chrome, % File_ChromeExe
Version_Chrome_ := RegexReplace(Version_Chrome, "\.\d+$")
Version_Chromedriver_ := RegexReplace(Version_Chromedriver, "\.\d+$")
; Check if versions are equal
if (Version_Chrome_=Version_Chromedriver_){
; Versions are the same
Return true
MsgBox,68,ChromeDriver,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_" Version_Chrome_, true)
oHTTP.Send()
oHTTP.WaitForResponse()
Version_Chromedriver := oHTTP.ResponseText
if InStr(Version_Chromedriver, "NoSuchKey"){
MsgBox,16,Testing,Error`nVersion_Chromedriver
return
}
; Download the Chromedriver
Url_ChromeDriver := "https://chromedriver.storage.googleapis.com/" 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")
FileCreateDir, Folder_Chromedriver "\Backup"
FileMoveDir, Dir_Chromedriver, Folder_Chromedriver "\Backup\", 1
FileObj := FolderObj.ParseName("chromedriver.exe")
AppObj.Namespace(Folder_Chromedriver "\").CopyHere(FileObj, 4|16)
; Clean up the zipfile
FileDelete, % A_ScriptDir "\chromedriver_win32.zip"
return true
}
RunHide(Command) {
dhw := A_DetectHiddenWindows
DetectHiddenWindows, On
Run, %ComSpec%,, Hide, cPid
WinWait, ahk_pid %cPid%
DetectHiddenWindows, %dhw%
DllCall("AttachConsole", "uint", cPid)
Shell := ComObjCreate("WScript.Shell")
Exec := Shell.Exec(Command)
Result := Exec.StdOut.ReadAll()
DllCall("FreeConsole")
Process, Close, %cPid%
Return Result
}
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 06 May 2022, 15:24
by RaptorX
AHK_user wrote: ↑06 May 2022, 13:49
It does some assumptions about the location of the ChromeDriver.exe and Chrome.exe, but I think they are acceptable.
I think the exact location of browsers can be found on the registry. I had some code a while ago that retrieved the defaults browser location but it could be tweaked to find the exact location of any installed program.
Re: Rufaydium WebDriver (no selenium/websocket)
Posted: 06 May 2022, 16:00
by AHK_user
RaptorX wrote: ↑06 May 2022, 15:24
AHK_user wrote: ↑06 May 2022, 13:49
It does some assumptions about the location of the ChromeDriver.exe and Chrome.exe, but I think they are acceptable.
I think the exact location of browsers can be found on the registry. I had some code a while ago that retrieved the defaults browser location but it could be tweaked to find the exact location of any installed program.
@RaptorX: Thanks for the tip, I found the location and updated my previous post.
Code: Select all
RegRead, File_ChromeExe, % "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Chrome.exe"