概述
WebDriver协议基于HTTP,使用JSON进行数据传输,定义了client与driver之间的通信标准。无论client的实现语言(如Java或C#),都能通过协议中的endpoints准确指示driver执行各种操作,覆盖了Selenium的所有功能。这些endpoints详细列出了所有可用的功能点。
目标
启动
bash
msedgedriver.exe --port=8080 --remote-allow-origins=* - --verbose -allow-insecure-localhost --allowed-origins=* --allowed-ips=["127.0.0.1"] --headless --disable-gpu
示例
#include "webdriver.h"
int main() {
WebDriver m_driver;
m_driver.NavigateTo("https://www.hao123.com/");
qDebug() << m_driver.getPageSource();
}
效果
webdriver.h
cpp
#ifndef WEBDRIVER_H
#define WEBDRIVER_H
#include <QNetworkAccessManager>
#include <QNetworkReply>
class WebDriver : public QObject
{
Q_OBJECT
public:
WebDriver();
~WebDriver();
void getStatus();
void getTimeout();
void setTimeOut(int implicit, long long pageLoad, long long script);
void NavigateTo(const QString& url);
QString getCurrentUrl();
void historyBack();
void historyForward();
void refresh();
QString getTitle();
QString showWindow();
void closeWindow();
void getWindowHandles();
void createWindow();
QString getPageSource();
signals:
void response(const QByteArray& bytes);
private:
void connectToServer(const QUrl& url);
private:
void post(const QByteArray& bytes);
void get();
void deleteRes();
private:
void waitFinish();
private:
QString m_host;
QNetworkAccessManager m_networkManger;
QNetworkRequest m_client;
std::string m_sessionID;
std::string m_elementID;
QByteArray m_responseCache;
};
#endif // WEBDRIVER_H
webdriver.cpp
cpp
#include "webdriver.h"
#include <QElapsedTimer>
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include "webDriverResponseJson.h"
#include "webDriverRequestJson.h"
WebDriver::WebDriver() : m_host("http://127.0.0.1:8080"){
connectToServer(QUrl(m_host + "/session"));
}
WebDriver::~WebDriver() {
if (m_sessionID.empty())
return;
m_client.setUrl(QUrl(m_host + QString("/session/") + QString::fromStdString(m_sessionID)));
deleteRes();
waitFinish();
}
void WebDriver::connectToServer(const QUrl& url) {
m_client.setUrl(url);
m_client.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8");
m_client.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0");
m_client.setRawHeader("Cache-Control", "no-cache");
REQ::Options options;
options.capabilities.browserName = "edge";
std::string json = JS::serializeStruct(options, JS::SerializerOptions(JS::SerializerOptions::Compact));
post(QString::fromStdString(json).toLocal8Bit());
waitFinish();
// 反序列
RES::MSEdgeRoot response;
JS::ParseContext parseContext(m_responseCache.data());
auto error = parseContext.parseTo(response);
if (JS::Error::NoError != error) {
std::string errorStr = parseContext.makeErrorString();
fprintf(stderr, "Error parsing struct %s\n", errorStr.c_str());
}
m_sessionID = response.value.sessionId;
}
void WebDriver::NavigateTo(const QString& url) {
m_client.setUrl(QUrl(m_host + QString("/session/") + QString::fromStdString(m_sessionID) + "/url"));
REQ::NavigateTo nav;
nav.url = url.toStdString();
std::string json = JS::serializeStruct(nav, JS::SerializerOptions(JS::SerializerOptions::Compact));
post(QString::fromStdString(json).toLocal8Bit());
waitFinish();
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(m_responseCache, &jsonError); // 转化为 JSON 文档
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
if (doucment.isObject()) { // JSON 文档为对象
QJsonObject object = doucment.object(); // 转化为对象
if (object.contains("value")) { // 包含指定的 key
QJsonValue value = object.value("value"); // 获取指定 key 对应的 value
qDebug() << "NavigateTo " << (value.isNull() ? "Success" : "Fail");
}
}
}
}
QString WebDriver::getPageSource() {
m_client.setUrl(QUrl(m_host + QString("/session/") + QString::fromStdString(m_sessionID) + "/source"));
get();
waitFinish();
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(m_responseCache, &jsonError); // 转化为 JSON 文档
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
if (doucment.isObject()) { // JSON 文档为对象
QJsonObject object = doucment.object(); // 转化为对象
if (object.contains("value")) { // 包含指定的 key
QJsonValue value = object.value("value"); // 获取指定 key 对应的 value
if (value.isString())
return value.toString();
}
}
}
return QString::fromStdString(m_responseCache.toStdString());
}
void WebDriver::waitFinish() {
QEventLoop loop;
connect(this, &WebDriver::response, &loop, &QEventLoop::quit);
loop.exec();
}
void WebDriver::post(const QByteArray& bytes) {
auto reply = m_networkManger.post(m_client, bytes);
connect(reply, &QNetworkReply::finished, [this, reply]() {
m_responseCache = reply->readAll();
qDebug() << "finish:" << m_responseCache;
emit response(m_responseCache);
});
}
void WebDriver::get() {
auto reply = m_networkManger.get(m_client);
connect(reply, &QNetworkReply::finished, [this, reply]() {
m_responseCache = reply->readAll();
qDebug() << "finish:" << m_responseCache;
emit response(m_responseCache);
});
}
void WebDriver::deleteRes() {
auto reply = m_networkManger.deleteResource(m_client);
connect(reply, &QNetworkReply::finished, [this, reply]() {
emit response(m_responseCache);
});
}
example.py
python
import requests
import json
session_url = 'http://localhost:8080/session'
session_pars ={"capabilities": {"browserName": "edge"}}
r_session = requests.post(session_url,json=session_pars)
print(json.dumps(r_session.json(),indent=2))
参考
使用 WebDriver 自动执行 Microsoft Edge - Microsoft Edge Developer documentation | Microsoft Learn WebDriver
selenium ,webdriver 运行原理与机制-腾讯云开发者社区-腾讯云
C/C++ JSON ORM 之 structs 数据结构相互嵌套定义-CSDN博客