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中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...
随机推荐
- [git 学习篇]远程创库
实际情况往往是这样,找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交. 完全 ...
- [adb 学习篇] adb常用命令
https://testerhome.com/topics/2565 Android 常用 adb 命令总结 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb ...
- POJ 2033 Alphacode
Alphacode Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11666 Accepted: 3564 Descri ...
- Problem 1004: 蛤玮打扫教室(区间覆盖端点记录)
Problem 1004: 蛤玮打扫教室 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %l ...
- [luoguP1251] 餐巾计划问题(费用流)
传送门 模型 网络优化问题,用最小费用最大流解决. 实现 把每天分为二分图两个集合中的顶点Xi,Yi,建立附加源S汇T. 1.从S向每个Xi连一条容量为ri,费用为0的有向边. 2.从每个Yi向T连一 ...
- 关闭chrome浏览器的input香蕉黄背景
chrome浏览器input的自动完成,点击之后自动输入,input的背景会变成香蕉黄,用如下方法修复: /* Change the white to any color ;) 就是给input设置内 ...
- LCT(Link-Cut Tree)
Link-cut tree(LCT)[可以理解为树链剖分+splay] 给出如下定义: access(x):访问x节点 perferred child:若以x为根的子树中最后被访问的节点在以x的儿子y ...
- ActiveMQ使用经验与优化
摘自:http://blog.csdn.net/m13321169565/article/details/8081314 1.1 不要频繁的建立和关闭连接 JMS使用长连接方式,一个程序,只要和JMS ...
- [USACO Section 2.1]城堡 The Castle (搜索)
题目链接 Solution 比较恶心的搜索,思路很简单,直接广搜找联通块即可. 但是细节很多,要注意的地方很多.所以直接看代码吧... Code #include<bits/stdc++.h&g ...
- La 3942 字符串+dp
题目大意:一个字符串,可以分解成若干英语单词的连接(单词可以重复使用),求有多少种方法? #include<iostream> #include<cstdio> #includ ...