将Selenium WebDriver连接到现有的浏览器会话

时间:2017-05-25 17:38:08

标签: java selenium-webdriver selenium-chromedriver

我正在使用selenium,如果现有浏览器会话(对我来说Chrome)当前存在,我想附加一个webdriver实例。我不想打开新的浏览器窗口/会话。我用google搜索并看到,有一些方法可以通过这些网站上的描述来实现这一点:

  1. Reuse existing Browser Session by extending RemoteWebDriver
  2. Basic Stuff for ChromeDriverService
  3. 我在最新版本中使用ChromeDriver 2.29

    我的代码现在看起来如下:

    public static void main(String[] args) throws Exception {
        // starting Chrome Webdriver server
        ChromeDriverService service = new ChromeDriverService.Builder()
                .usingDriverExecutable(new File("D:\\Development\\chromedriver\\chromedriver.exe"))
                .usingAnyFreePort()
                .build();
        service.start();
    
        WebDriver driver = new CustomRemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
        driver.get("http://www.google.com");
        WebDriver driver2 = new CustomRemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
        // here I am expecting www.google.com from last driver instance, because it should have the same session
        System.out.println(driver2.getCurrentUrl());
        driver.quit();
        driver2.quit();
    }
    

    扩展的RemoteWebDriver,用于检查会话是否已存在:

    public class CustomRemoteWebDriver extends RemoteWebDriver {
        public static String sessiondIdPath = "c:\\automation\\sessionid";
    
        public CustomRemoteWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
            super(remoteAddress, desiredCapabilities);
        }
    
        @Override
        protected void startSession(Capabilities desiredCapabilities) {
            String sid = getPreviousSessionIdFromSomeStorage();
            if (sid != null) {
                setSessionId(sid);
                try {
                    getCurrentUrl();
                } catch (WebDriverException e) {
                    // session is not valid
                    e.printStackTrace();
                    sid = null;
                }
            }
    
            if (sid == null) {
                super.startSession(desiredCapabilities);
                saveSessionIdToSomeStorage(getSessionId().toString());
            }
        }
    
        private void saveSessionIdToSomeStorage(String sessionId) {
            try {
                FileUtils.writeStringToFile(new File(sessiondIdPath), sessionId, Charset.defaultCharset());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private String getPreviousSessionIdFromSomeStorage() {
            String sessionId;
            try {
                List<String> sidText = FileUtils.readLines(new File(sessiondIdPath), Charset.defaultCharset());
                sessionId = sidText.get(0);
            } catch (Exception e) {
                return null;
            }
            return sessionId;
        }
    }
    

    和pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.andy.selenium</groupId>
        <artifactId>remotewebdriverexample</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.4.0</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.5</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
            <dependency>
                <groupId>io.appium</groupId>
                <artifactId>java-client</artifactId>
                <version>4.1.2</version>
            </dependency>
        </dependencies>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.6.1</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    假设我当前没有保存sessionId:启动driver1并转到www.google.com按预期工作。在driver2的初始化时,CustomRemoteWebDriver#startSession中抛出了WebDriverException。所以它正在打印堆栈跟踪,sessionId似乎无效。 stacktrace的输出如下:

      

    启动ChromeDriver 2.29.461591   (62ebf098771772160f391d75e589dc567915b233)在31495港口只有当地的   允许连接。 Mai 25,2017 6:53:14 PM   org.openqa.selenium.remote.ProtocolHandshake createSession   信息:检测到的方言:OSS

         

    org.openqa.selenium.WebDriverException:没有命令或响应编解码器   已定义。无法继续构建信息:版本:'3.4.0',   版本:'未知',时间:'未知'系统信息:主持人:   'DESKTOP-AGEFV4C',ip:'192.168.134.1',os.name:'Windows 10',   os.arch:'amd64',os.version:'10 .0',java.version:'1.8.0_121'驱动程序   info:driver.version:CustomRemoteWebDriver at   org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:154)     在   org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)     在   org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:694)     在   org.openqa.selenium.remote.RemoteWebDriver.getCurrentUrl(RemoteWebDriver.java:374)     在CustomRemoteWebDriver.startSession(CustomRemoteWebDriver.java:57)     在   org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:137)     在   org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:174)     在CustomRemoteWebDriver。(CustomRemoteWebDriver.java:22)at   RemoteBrowserConnector.start(RemoteBrowserConnector.java:40)at   RemoteBrowserConnector.main(RemoteBrowserConnector.java:26)Mai 25,   2017年下午6:53:18 org.openqa.selenium.remote.ProtocolHandshake   createSession信息:检测到的方言:OSS

    我已经通过更新this帖子建议的java客户端来尝试它,但它没有帮助。由于此异常,始终会打开一个新窗口。任何想法,我做错了什么?

0 个答案:

没有答案