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();
}
}

Selenium系列之--08 操作已打开的浏览器的更多相关文章

  1. 使用python+selenium控制手工已打开的浏览器

    我们可以利用Chrome DevTools协议.它允许客户检查和调试Chrome浏览器. 打开cmd,在命令行中输入命令: chrome.exe --remote-debugging-port=922 ...

  2. 用selenium控制已打开的浏览器

    在使用selenium进行自动化测试会遇到,手工打开浏览器,做了一部分操作后,并打开相关页面后再执行相关的自动化脚本. 如何使用selenium来接管先前已打开的浏览器呢?醍提出一个Google Ch ...

  3. selenium+python自动化105 - selenium 如何在已打开的浏览器上继续运行自动化脚本?

    前言 使用selenium 做web自动化的时候,经常会遇到这样一种需求,是否可以在已经打开的浏览器基础上继续运行自动化脚本? 这样前面的验证码登录可以手工点过去,后面页面使用脚本继续执行,这样可以解 ...

  4. Selenium系列之--07 操作远程浏览器

    Selenium远程控制浏览,可以通过如下两种方式实现,本质上都是Selenium Grid a.  客户机启Selenium Standalone Server 作为远程服务,服务端通过调用Remo ...

  5. selenium 3.0与2.0 打开火狐浏览器的区别

    3.0的selenium需要引入gecko.driver驱动 ,因为没有在系统环境path中配置相关路径,因此需要特别指出,为了方便使用,建议直接和火狐安装包中的.exe文件放在同一目录下. 2.0的 ...

  6. selenium+python自动化之操作浏览器

    一.打开网站 1.第一步:从selenium里面导入webdriver模块 2.打开Firefox浏览器(Ie和Chrome对应下面的) 3.打开百度网址 二.设置休眠 1.由于打开百度网址后,页面加 ...

  7. selenium在Eclipse中打开fireFox浏览器是报报错connect to host 127.0.0.1 on port 7055

    1.相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器是报报错: org.openqa.selenium.firefox.NotConnectedException: U ...

  8. Selenium系列(二) - 控制浏览器操作的详细解读

    如果你还不想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识 ...

  9. Outlook 2007无法打开链接"由于本机的限制 该操作已被取消"

    编写人:CC阿爸 2014-2-17 近来在日常维护中,经常性的遇到用户在outlook中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...

随机推荐

  1. pip installl安装包特别慢? 指定源进行安装

    指定源地址安装: pip install -i http://pypi.douban.com/simple/ packagename pip install -i http://pypi.tuna.t ...

  2. EF的三种模式

    1.DateBase First(数据库优先) 2.Model First(模型优先) 3.Code First(代码优先) 当然,如果把Code First模式的两种具体方式独立出来,那就是四种了. ...

  3. tcpdump 进行抓包

    tcpdump 进行抓包是怎么回事? tcp抓包是怎么搞的?

  4. [Err] 1022 - Can't write; duplicate key in table '#sql-1500_26'

        今天用powerdesigner修改了一些外键关系,有两个外键的名字取一样的,忘记改了.然后在用navicat运行sql文件时,报出[Err] 1022 - Can't write; dupl ...

  5. hdu6098[RMQ+筛法] 2017多校6

    /*hdu6098[RMQ+筛法] 2017多校6*/ #include <bits/stdc++.h> using namespace std; ][], len[], a[]; voi ...

  6. 【bzoj4260】Codechef REBXOR Trie树

    题目描述 输入 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. 输出 输出一行包含给定表达式可能的最大值. 样例输入 5 1 2 3 1 2 样例输出 ...

  7. BFC浅析

    1.定义 BFC(Block formatting context)即"块级格式化上下文".它是一个独⽴的渲染区域,只有Block-level box参与, 它规定了内部的Bloc ...

  8. [暑假集训--数位dp]hdu5898 odd-even number

    For a number,if the length of continuous odd digits is even and the length of continuous even digits ...

  9. 关于python数据类型的一些举例

    if True: name2=1 print(name2) 输出:1 if False: name1=1 print(name1) 报错: NameError: name 'name1' is not ...

  10. 【CF1015B】Obtaining the String(模拟)

    题意:给定两个字符串,每次可以交换相邻两个字符,给出任意一组交换次数小于1e4的方案使得a串成为b串,输出交换的次数与位置,无解输出-1 n<=50 思路:每次找到第一个不相同的字符,从后面找到 ...