Here is how to use Chromedriver without libraries like selenium-webdriver
. This can be useful for debugging.
The following example visits a web page and reads the a headline's text contents.
-
Create a session. You will get a JSON response containing lots of information about your Chrome session, including a
sessionId
. Use that to send any future commands to your chromedriver session.$ curl -XPOST http://localhost:9515/session -d '{"desiredCapabilities":{"browserName":"chrome"}}' {"sessionId":"your-session-id-here","status":0,"value":{...}}
You might want to pass options as into the
chromeOptions
capabilities e.g.--headless
:'{"desiredCapabilities":{"browserName":"chrome", "chromeOptions":{"args":["--headless"]}}}'
-
Visit a URL like this:
$ curl http://localhost:9515/session/your-session-id-here/url -d '{"url":"http://example.com/"}' {"sessionId":"...","status":0,"value":null}
-
Finding elements is fairly simple:
$ curl http://localhost:9515/session/your-session-id-here/element -d '{"using":"tagName","value":"h1"}' {"sessionId":"...","status":0,"value":{"ELEMENT":"element-object-id-here"}}
-
Read the element's text content by using the element's identifier received above:
$curl http://localhost:9515/session/your-session-id-here/element/element-object-id-here/text {"sessionId":"...","status":0,"value":"Example Domain"}
-
To close Chrome, terminate your session:
$curl -XDELETE http://localhost:9515/session/your-session-id-here {"sessionId":"...","status":0,"value":null}
Chromedriver follows the W3C WebDriver spec. There are tons of things you can do with it, but performing more advanced tasks without a tool like selenium-webdriver
can be quite difficult.
However, for simple debugging or remote-controlling, curl might be an adequate option.