zoukankan      html  css  js  c++  java
  • Selenium系列之--08 操作已打开的浏览器

    参考文章:Can Selenium interact with an existing browser session?

    1. 建一个ReuseWebDriver类

    import java.io.IOException;
    import java.net.URL;
    
    import org.openqa.selenium.remote.Command;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.HttpCommandExecutor;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.remote.Response;
    
    public class ReuseWebDriver extends RemoteWebDriver {
    
        public ReuseWebDriver(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());
        }
    }

    2. 调用

    public class ReuseIEDriverTest {
    
        public static void main(String[] args) throws MalformedURLException {
            -------
            // 初始化一个IE浏览器实例
            InternetExplorerDriver driver = new InternetExplorerDriver(
                    ieCapabilities);
            // 最大化窗口
            driver.manage().window().maximize();
            // get()打开一个站点
            driver.get("https://www.baidu.com");
            // getTitle()获取当前页面title的值
            System.out.println("当前打开页面的标题是: " + driver.getTitle());
            SessionId sessionId = driver.getSessionId();
            URL url = ((HttpCommandExecutor) driver.getCommandExecutor())
                    .getAddressOfRemoteServer();
            System.out.println(sessionId);
            System.out.println(url);
    
            // 初始化一个chrome浏览器实例,实例名称叫driver
            ReuseWebDriver2 redriver = new ReuseWebDriver2(url,sessionId.toString());
            // get()打开一个站点
            redriver.get("https://www.bing.com");
            // getTitle()获取当前页面title的值
            System.out.println("当前打开页面的标题是: " + redriver.getTitle());
            System.out.println(redriver.getSessionId());
            System.out.println(redriver.getCapabilities());
            System.out.println(((HttpCommandExecutor) redriver.getCommandExecutor())
                    .getAddressOfRemoteServer());
            redriver.executeScript("alert("hello,this is an alert!")");
            // 关闭并退出浏览器
            // driver.quit();
        }
    }
  • 相关阅读:
    POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)
    HTTP协议详解??
    Python 中三大框架各自的应用场景??
    django 开发中数据库做过什么优化??
    谈一下你对 uWSGI 和 nginx 的理解??
    django 中间件的使用??
    Flask 中请求钩子的理解和应用?
    七层模型? IP ,TCP/UDP ,HTTP ,RTSP ,FTP 分别在哪层?
    说说 HTTP 和 HTTPS 区别??
    hasattr() getattr() setattr() 函数使用详解??
  • 原文地址:https://www.cnblogs.com/liuyitan/p/10542772.html
Copyright © 2011-2022 走看看