18

We need to do some fairly complex web automation from C++ application (log into application, do some actions, logout), but performance is really important so we are looking at options.

  1. Is there a way to drive WebKit or other headless engine directly from C++, without the need for few more layers in between (like selenium+webdriver+network communication+...)? Chromedriver perhaps?

  2. If option 1 is not possible, what is the most optimal way to run WebDriver (with real browser) from C++?

bozo
  • 947
  • 1
  • 13
  • 33

2 Answers2

23

You can use selenium server and JsonWireProtocol. In C++ you can implement CURL requests to selenium server and do web automation with C++.

Use this link first: My fork of Webdriver++.

There are also some C++ libraries that do this work. The first is Webdriver++ By sekogan but last commit was 3 years ago, and it seems not all things works for now. The second is my fork of Webdriver++, i've fixed some bugs and make this project as shared library, so you can use it in any C++ project.

This is an example of how you can use my My fork of Webdriver++.

#include <webdriverxx/webdriverxx.h>
using namespace webdriverxx;

int main() {
   WebDriver firefox = Start(Firefox());
   firefox
       .Navigate("http://google.com")
       .FindElement(ByClass("class_name"))
       .SendKeys("Hello, world!")
       .Submit();
   return 0;    
}
idurdyev
  • 371
  • 3
  • 8
  • 4
    Your fork is very sleek (very easy to use and clean!). Tho, according to your README, it seems like selenium server is still required. I just tried and your example would attempt to connect to localhost:4444. It seems like selenium server will need to be run as a process listening to port 4444. I just wonder whether there is an even more lightweight solution that doesn't require to pass messages through another process but can navigate website with javascript/ajax, etc. Thanks in advance! – HCSF Nov 25 '18 at 16:21
  • I tested the driver and it works fine. Is there any way to use the driver without actually starting real browser? – Salhi Fedi Dec 17 '22 at 22:08
8

You should look into PhantomJS (a headless WebKit browser), which comes with GhostDriver, which is the WebDriver protocol implementation for PhantomJS.

You will still need to use one of the WebDriver language bindings, which I'm not aware of any of the language bindings that are in C++, but perhaps one of the available languages could be used by your team for automation purposes.

Worst case, you could always create your WebDriver script in Python, and call the Python script from your C++ application.

Nathan Dace
  • 1,545
  • 9
  • 21
  • 2
    @bozo If you really don't care that you're not running in a "real" browser, then directly consuming QtWebKit might be a good choice. Realize, though that it's not a trivial undertaking. There aren't any C++ language bindings for WebDriver, as far as I know, but as long as you have a JSON parsing library (json-cpp is pretty good), and an HTTP client library, you can write your own language bindings in pretty short order. – JimEvans Jun 27 '13 at 23:37
  • 3
    Hi there. Do you think there is any performance advantages worth mentioning if WebDriver binding in C++ as opposed to using some scripting language with existing WebDriver bindding? I am looking for high performance and SO want to avoid Java. – bozo Jun 28 '13 at 15:00