我正在寻找一种方法将现有的webdriver浏览器会话重新连接到驱动程序实例,以便我可以再次控制浏览器。
到目前为止,我已经尝试了以下内容:
我。 ========================================= =
参考:https://stackoverflow.com/a/38827934/2285470
浏览器:Firefox v.51.01 / Chrome v.56.0.2924.87
驱动程序: geckodriver v.1.14 / chromedriver v.2.27
已尝试解决方案:
1.创建一个扩展RemoteWebDriver的自定义类
public class RemoteDriver extends RemoteWebDriver {
public RemoteDriver(URL url, String sessionId) {
super();
setSessionId(sessionId);
setCommandExecutor(new HttpCommandExecutor(url) {
@Override
public Response execute(Command command) throws IOException {
if (command.getName() != "newSession") {
return super.execute(command);
}
return super.execute(new Command(getSessionId(), "getCapabilities"));
}
});
startSession(new DesiredCapabilities());
}
}
使用以下命令调用扩展RemoteWebDriver的自定义类:
RemoteDriver cdriver = new RemoteDriver(new URL("http://localhost:7055/hub"),"my-session-id");
结果 org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed
在return super.execute(new Command(getSessionId(), "getCapabilities"));
II。 ====================================== ====
参考: https://groups.google.com/forum/#!topic/selenium-developers/1LygDvlQ3H4
浏览器:Firefox v.51.01 / Chrome v.56.0.2924.87
驱动程序: geckodriver v.1.14 / chromedriver v.2.27
已尝试解决方案:
1.创建一个扩展RemoteWebDriver的自定义类
public class RemoteDriver extends RemoteWebDriver {
@Override
protected void startSession(Capabilities desiredCapabilities) {
String sid = getSessionIdFromPropertyFile();
if (sid != null) {
setSessionId(sid);
try {
super.startSession(desiredCapabilities);
} catch (WebDriverException e) {
// session is not valid
sid = null;
}
}
if (sid == null) {
super.startSession(desiredCapabilities);
saveSessionIdToPropertyFile(getSessionId().toString());
}
}
}
使用以下命令调用扩展RemoteWebDriver的自定义类:
RemoteDriver cdriver = new RemoteDriver();
cdriver.startSession(DesiredCapabilities.firefox());
cdriver.get("https://www.google.com");
结果: org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
似乎其他人可以让它工作,但经过几个小时的尝试后,我仍然无法做到这一点。
我正在使用selenium-java 3.1.0版
提前致谢。