Hello!
Is there a possibility to communicate with browser (e.g. using webdriver)?
They use Remote Webdriver which is a RESTful / JSON Protocol named: wire protocol.
https://code.google.com/p/selenium/wiki/JsonWireProtocol
There are standalone "Webdrivers" for all major browsers which simply open a port listening to the wire protocol and "remote control" their Browser.
Selenium can interface those and has binding for Java, C#, Python, PHP and Perl. I skimmed over the PHP one and think that this is more a time consuming than a hard to do task to create Nim support if one uses some existing higher level stuff like json parser for example.
There are no c/c++ bindings for this afaik. I am not sure about the Selenium RC API but using those WebDriver(s) is probably enough, depending on what you wanna do.
I just played for some minutes with the ChromeDriver and PAW (https://luckymarmot.com/paw a very very good tool to test / emulate RESTful APIs) and just surfed to google and took a screen of the website with it :)
From what I saw it is pretty easy to do most stuff, including injecting and running java-script or getting an element or just the source of the page.
a RESTful / JSON Protocol
Then httpclient + json will do.
And the protocol is here: Webdriver Spec (Just scroll down until you see a table in section "List of Endpoints")
Well you find quite a lot of documentation about it:
https://sites.google.com/a/chromium.org/chromedriver/getting-started
Basically you download the Google Chrome webdriver:
https://sites.google.com/a/chromium.org/chromedriver/downloads
then run it... and use something like this to "control" it:
# nim c --hints:off --verbosity:0
import httpclient
import json
import os
import strutils
const endpoint = "http://127.0.0.1:9515"
var headers = ""
headers &= "Content-Type: application/json; charset=utf-8\c\L"
let statusResponse = get(endpoint / "status", headers)
echo $statusResponse.status
let jsonStatus = parseJSON(statusResponse.body)
if jsonStatus == nil:
echo "could not query status"
quit 5
# play with the answer
#echo $json_status
if jsonStatus["status"].getNum != 0:
echo "status returned unexpected " & $jsonStatus["status"]
quit 5
try:
let values = json_status["value"]
echo values["build"]["version"]
echo values["os"]["name"]
echo values["os"]["arch"]
echo values["os"]["version"]
except:
echo "Missing information from Status request"
# now we create a session
var data = %* {
"desiredCapabilities": {"takesScreenshot": true}
}
let sessionResponse = post(endpoint / "session", headers, $data)
echo sessionResponse.status
let jsonSession = parseJSON(sessionResponse.body)
let tmp = jsonSession.getOrDefault("sessionId")
if tmp == nil:
echo "we did not get a session id. check you webdriver is running on " & endpoint
quit 5
#echo $jsonSession
let sessionId = tmp.str
echo "we have a session id (" & sessionId & ")"
# so lets open nim-lang.org in the browser
data = %* {
"url": "http://nim-lang.org/"
}
var response = post(endpoint / "session" / sessionId / "url", headers, $data)
echo $response.status
echo $parseJSON(response.body)
# and so on...
Here how to close the session (I struggled at first on how to do it)
# add something this after the code of my last post
sleep(5_000)
response = request(endpoint / "session" / sessionId, httpDELETE, headers)
echo $response.status
# and so on...
And here is a Nim package that I wrote: https://github.com/dom96/webdriver (used by this forum for testing).
Btw why are you bumping this thread? It's more than 4 years old.
Webdriver also targets a newer protocol than Halonium too IIRC.
@dom96, that is not correct. Halonium supports both the old protocol and the new one that webdriver supports (just like Python's selenium).
@Lecale, but dom is right. You can use whichever one :)
Good point, I added some links to examples.
I will probably add the set up code used in those examples into the library as well to make the example code shorter/simpler.
Awesome thank you. I was having problems with the gecko driver on Mac, apparently there's some validation issue with Mac Catalina.
Anyways, used the chromedriver and it works like a charm!
If anyone has any problems getting chromedriver working just @me here. Happy to try helping walk you through it.