Question, how can I set the user-agent?
![Thinking :think:](./images/smilies/icon_think.gif)
Something with ChromeDriver.exe --user-agent ?
@joedf
Code: Select all
static ChromeCustomUserAgent =
( LTrim Join
{
"capabilities": {
"alwaysMatch": {
"browserName": "chrome",
"platformName": "windows",
"goog:chromeOptions": {
"w3c": json.true,
"args" :["--user-agent=Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"],
"excludeSwitches": ["enable-automation"]
}
},
"firstMatch": [{}]
},
"desiredCapabilities": {
"browserName": "chrome"
}
}
)
Code: Select all
f1::
Driver := new RunDriver(ChromeDriver) ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
Chrome.capabilities := Capabilities.ChromeCustomUserAgent ; loading custom capabilities
; msgbox, % json.dump(Chrome.capabilities)
Page := Chrome.NewSession()
Page.Navigate("https://www.google.com/")
Page := ""
return
Hello,
Listline displaysGS := Chrome.getSessionByUrl("https://www.google.com/")
Code: Select all
0: Sessions := this.send(this.DriverUrl "/sessions","GET")
034: if !instr(url,"HTTP")
036: r := Json.load(Request(url,Method,Payload,WaitForResponse)).value
---- C:\TEMP\Rufaydium\lib\WDM.ahk
050: WebRequest.Open(Method, url, false)
051: WebRequest.SetRequestHeader("Content-Type","application/json")
052: if Payload
055: WebRequest.Send() (0.03)
056: if WaitForResponse
057: WebRequest.WaitForResponse()
058: Return,WebRequest.responseText
---- C:\TEMP\Rufaydium\lib\JSON.ahk
261: this._init()
065: if (this.lib)
066: Return
263: _json := " " json
264: VarSetCapacity(pJson, A_PtrSize)
265: NumPut(&_json, &pJson, 0, "Ptr")
267: VarSetCapacity(pResult, 24)
269: if (r := DllCall(this.lib.loads, "Ptr", &pJson, "Ptr", &pResult , "CDecl Int")) || ErrorLevel
276: result := ComObject(0x400C, &pResult)[]
277: if (IsObject(result))
278: ObjRelease(&result)
279: Return,result
---- C:\TEMP\Rufaydium\lib\Rufaydium.ahk
037: if r
038: Return,r
071: windows := []
072: For k,se in Sessions
074: chromeOptions := Se["capabilities","goog:chromeOptions"]
075: s := []
076: s.id := Se.id
077: s.debuggerAddress := StrReplace(chromeOptions.debuggerAddress,"localhost","http://127.0.0.1")
078: s.address := this.DriverUrl "/session/" s.id
079: windows[k] := new Session(s)
118: this.id := i.id
119: this.Address := i.address
120: this.debuggerAddress := i.debuggerAddress
121: this.currentTab := this.Send("window","GET")
034: if !instr(url,"HTTP")
035: url := this.address "/" url
036: r := Json.load(Request(url,Method,Payload,WaitForResponse)).value
---- C:\TEMP\Rufaydium\lib\WDM.ahk
050: WebRequest.Open(Method, url, false)
051: WebRequest.SetRequestHeader("Content-Type","application/json")
052: if Payload
055: WebRequest.Send() (60.19)
Code: Select all
/*
html code:
<select id='selform' size=4>
<option>
<option value='ca'>Canada
<option value='fr'>France
<option value='uk'>United Kingdom
</select>
*/
dropdown := GS.GetelementById("selform")
MsgBox % dropdown.innertext ; works
dropdown.selectByIndex(1)
dropdown.selectByVisibleText("Canada")
dropdown.selectByValue("fr")
If you use the posted example script f12 is a hotkey at the bottom of the script
Code: Select all
...
f12:: ; close all session first then exit driver
Driver := new RunDriver(ChromeDriver)
Chrome := new Rufaydium(Driver)
Chrome.QuitAllSessions()
driver.exit()
return
Thanks, I had overlooked the hotkeyahk7 wrote: ↑19 Apr 2022, 14:33If you use the posted example script f12 is a hotkey at the bottom of the scriptCode: Select all
... f12:: ; close all session first then exit driver Driver := new RunDriver(ChromeDriver) Chrome := new Rufaydium(Driver) Chrome.QuitAllSessions() driver.exit() return
Just Approved and merged your pull request I am exploring github and learning stuff. thank youahk7 wrote: ↑19 Apr 2022, 14:16@Xeo786 I've submitted a pull request to fix some typos, you can see the changes here https://github.com/Xeo786/Rufaydium-Webdriver/pull/1/files
Just as note: you can also add images to your GitHub repository so you don't have to rely on a third party image hosting provider.
by using this technique I can easily implement selectByIndex(), selectByVisibleText()and selectByValue()ahk7 wrote: ↑19 Apr 2022, 14:31Input text, checkbox, clicking buttons all OK but one thing that eludes me at the moment is selection options in a <select> - I can get the innertext so it does see the html but setting a value .selectByIndex .selectByVisibleText or .selectByValue doesn't seem to do much. Anybody have an idea?Code: Select all
/* html code: <select id='selform' size=4> <option> <option value='ca'>Canada <option value='fr'>France <option value='uk'>United Kingdom </select> */ dropdown := GS.GetelementById("selform") MsgBox % dropdown.innertext ; works dropdown.selectByIndex(1) dropdown.selectByVisibleText("Canada") dropdown.selectByValue("fr")
Code: Select all
Driver := new RunDriver(ChromeDriver) ; running driver
Chrome := new Rufaydium(Driver) ; this will return control over Browser
Chrome.capabilities := Capabilities.ChromeCustomUserAgent ; loading custom capabilities
; msgbox, % json.dump(Chrome.capabilities)
Page := Chrome.getSessionByUrl("https://www.w3.org/TR/wai-aria-practices-1.1/examples/listbox/listbox-collapsible.html")
; we can also do getSessionByTitle(title)
if !IsObject(Page) ; GS >> google session
{
Page := Chrome.NewSession()
Page.Navigate("https://www.w3.org/TR/wai-aria-practices-1.1/examples/listbox/listbox-collapsible.html")
}
msgbox, Fast Way
Page.Getelementbyid("exp_button").click()
Slist := Page.querySelector("#exp_elem_list")
for k, item in StrSplit(Slist.innerText,"`n") ; using strsplit to get to location this is fast
{
if instr(item,"Curium")
{
Slist.QuerySelectorAll("li")[k].click()
}
}
Page.Getelementbyid("exp_wrapper").click() ; clicking somewhere else to collapse dropdown
msgbox, slow way
Page.Getelementbyid("exp_button").click()
for k, li in Slist.QuerySelectorAll("li")
{
if instr(li.innerText,"Californium") ; asking for every element innerText take time
{
li.click()
}
}
Page.Getelementbyid("exp_wrapper").click() ; clicking somewhere else to collapse dropdown
Msgbox, done
If you feel leg while accessing session, close all session using Session.exit(), and Driver.exit() and reload driver
Code: Select all
BoundCallback := Func("Callback").Bind()
PageInstCreate := ChromeInst.GetPageByURL("file:///" Path, "exact",,BoundCallBack)
if !PageInstCreate
{
ChromeInst.Kill()
MsgBox, Could not Connect to Quote Page!
ExitFunc()
}else{
PageInstCreate.Call("Runtime.enable")
}
;Listen for messages here {console.logs()]
Callback(event){
GLOBAL
if(Event.Method = "Runtime.consoleAPICalled"){
}
}
There is a way you can access event using logs files generated by webdriver you can use these cmd argument like --log-file-path or --enable-chrome-logsTre4shunter wrote: ↑20 Apr 2022, 10:59Actually, I dont see a way to emulate Callbacks like we could in chrome.ahk.
Is this something that would be possible with Rufaydium? Looking online i see some examples for python/selenium related to monitoring console messages etc, but havent dug in too far yet.
What im referencing would be like so via chrome.ahk:
Code: Select all
BoundCallback := Func("Callback").Bind() PageInstCreate := ChromeInst.GetPageByURL("file:///" Path, "exact",,BoundCallBack) if !PageInstCreate { ChromeInst.Kill() MsgBox, Could not Connect to Quote Page! ExitFunc() }else{ PageInstCreate.Call("Runtime.enable") } ;Listen for messages here {console.logs()] Callback(event){ GLOBAL if(Event.Method = "Runtime.consoleAPICalled"){ } }
Hello,
I am trying to recreate the example code.
The code with the F1 key is ok. But as soon as I press F3 after pressing F1, Autohotkey hangs. The reason is
Code: Select all
GS := Chrome.getSessionByUrl("https://www.google.com/")
Hi,If you feel leg while accessing session, close all session using Session.exit(), and Driver.exit() and reload driver
the issue is when you manually close session created by driver, its not properly closed. and webdriver take time to query non exiting session
so make sure always close session using session.exit()
Code: Select all
if !IsObject(GS) ; GS >> google session
{
msgbox, Session with google tab no found
return
}
This has been discussed see linkMilchmann wrote: ↑21 Apr 2022, 01:51to the points:
- With your example, if I kill the driver at F1 with Driver.Exit(), then when I start F3 the following comes:What should the code be then if it should work without Autohotkey hanging? Please an example.Code: Select all
if !IsObject(GS) ; GS >> google session { msgbox, Session with google tab no found return }
- Session.exit() ( Page.exit() in the example ) does not exist.
- There is a problem with AutoHotkeyU64.exe and your code. Here I can partly not access the driver at all. Might be explainable, because Chromedriver.exe is 32 bit.
- querySelectorAll("a").length returns no value on google.com --> is there another way to get the length?
Thanks for your effort and I hope the project will run properly.
Bert
No such problems here, runs on Win7 32bit and Win10 64bit here. Try adding https://www.autohotkey.com/docs/commands/ListVars.htm after the GS := Chrome.getSessionByUrl("https://www.google.com/") line - if it doesn't show up as 'session' there might be something wrong indeed.Milchmann wrote:There is a problem with AutoHotkeyU64.exe and your code. Here I can partly not access the driver at all. Might be explainable, because Chromedriver.exe is 32 bit.