是否可以捕获已经打开的浏览器(chrome)的会话ID?

时间:2020-05-22 13:03:16

标签: java selenium selenium-webdriver automation selenium-chromedriver

在尝试通过硒自动化过程之前,我试图获取手动打开的Chrome浏览器的会话ID。 目前,我正在使用TARUN LALWANI提供的代码

Re-using existing browser session in Selenium using Java

public static RemoteWebDriver createDriverFromSession(final SessionId sessionId, URL command_executor){
    CommandExecutor executor = new HttpCommandExecutor(command_executor) {

@Override
public Response execute(Command command) throws IOException {
    Response response = null;
    if (command.getName() == "newSession") {
        response = new Response();
        response.setSessionId(sessionId.toString());
        response.setStatus(0);
        response.setValue(Collections.<String, String>emptyMap());

        try {
            Field commandCodec = null;
            commandCodec = this.getClass().getSuperclass().getDeclaredField("commandCodec");
            commandCodec.setAccessible(true);
            commandCodec.set(this, new W3CHttpCommandCodec());

            Field responseCodec = null;
            responseCodec = this.getClass().getSuperclass().getDeclaredField("responseCodec");
            responseCodec.setAccessible(true);
            responseCodec.set(this, new W3CHttpResponseCodec());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    } else {
        response = super.execute(command);
    }
    return response;
}
};
return new RemoteWebDriver(executor, new DesiredCapabilities());

}

public static void main(String [] args) {

ChromeDriver driver = new ChromeDriver();
HttpCommandExecutor executor = (HttpCommandExecutor) driver.getCommandExecutor();
URL url = executor.getAddressOfRemoteServer();
SessionId session_id = driver.getSessionId();

RemoteWebDriver driver2 = createDriverFromSession(session_id, url);
driver2.get("http://tarunlalwani.com");

}

但是似乎每次运行自动化程序时都会打开一个新的浏览器

我希望自动化检查窗口中是否已打开chrome浏览器,然后获取该会话ID并在其中打开新标签以通过硒启动自动化过程。 有可能吗?

谢谢

Akram

0 个答案:

没有答案