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();
}
}
Selenium系列之--08 操作已打开的浏览器的更多相关文章
- 使用python+selenium控制手工已打开的浏览器
我们可以利用Chrome DevTools协议.它允许客户检查和调试Chrome浏览器. 打开cmd,在命令行中输入命令: chrome.exe --remote-debugging-port=922 ...
- 用selenium控制已打开的浏览器
在使用selenium进行自动化测试会遇到,手工打开浏览器,做了一部分操作后,并打开相关页面后再执行相关的自动化脚本. 如何使用selenium来接管先前已打开的浏览器呢?醍提出一个Google Ch ...
- selenium+python自动化105 - selenium 如何在已打开的浏览器上继续运行自动化脚本?
前言 使用selenium 做web自动化的时候,经常会遇到这样一种需求,是否可以在已经打开的浏览器基础上继续运行自动化脚本? 这样前面的验证码登录可以手工点过去,后面页面使用脚本继续执行,这样可以解 ...
- Selenium系列之--07 操作远程浏览器
Selenium远程控制浏览,可以通过如下两种方式实现,本质上都是Selenium Grid a. 客户机启Selenium Standalone Server 作为远程服务,服务端通过调用Remo ...
- selenium 3.0与2.0 打开火狐浏览器的区别
3.0的selenium需要引入gecko.driver驱动 ,因为没有在系统环境path中配置相关路径,因此需要特别指出,为了方便使用,建议直接和火狐安装包中的.exe文件放在同一目录下. 2.0的 ...
- selenium+python自动化之操作浏览器
一.打开网站 1.第一步:从selenium里面导入webdriver模块 2.打开Firefox浏览器(Ie和Chrome对应下面的) 3.打开百度网址 二.设置休眠 1.由于打开百度网址后,页面加 ...
- selenium在Eclipse中打开fireFox浏览器是报报错connect to host 127.0.0.1 on port 7055
1.相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器是报报错: org.openqa.selenium.firefox.NotConnectedException: U ...
- Selenium系列(二) - 控制浏览器操作的详细解读
如果你还不想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识 ...
- Outlook 2007无法打开链接"由于本机的限制 该操作已被取消"
编写人:CC阿爸 2014-2-17 近来在日常维护中,经常性的遇到用户在outlook中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...
随机推荐
- Educational Codeforces Round 36 (Rated for Div. 2)
A. Garden time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- 【瓜分5000元奖金】Wannafly挑战赛13
链接:https://www.nowcoder.com/acm/contest/80/A来源:牛客网 zzy的小号 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...
- Welcome-to-Swift-18类型转换(Type Casting)
类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...
- Python Base One
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...
- 进程与multiprocessing模块
一 进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在 ...
- 安装phpssdb扩展:
安装 igbinary 扩展(安装phpssdb扩展时候要用到--enable-ssdb-igbinary): clone https://github.com/igbinary/igbinar ...
- inline关键词的使用(转载)
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- HDU 1754:I Hate It(线段树-单点更新)
题意: 1~N这些人有一些分数,之后有M条操作.要求支持两种操作:更新其中某个人的成绩,查询[A,B]区间内的人的最高成绩. ( 0<N<=200000,0<M<5000 ) ...
- hdu 1099(数学)
Lottery Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- elasticsearch入库错误:gc overhead导致数据节点脱离集群
https://my.oschina.net/u/3625378/blog/1793796