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中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...
随机推荐
- 指定特殊的安装目录用configure进行配置
linux - Make install, but not to default directories? - Stack Overflow I want to run 'make install' ...
- iOS启动动画效果实现
原理 在window上加一个UIImageView它的图片和启动图的图片一样,然后再调整动画 运行展示 demo百度云连接:http://pan.baidu.com/s/1c0QcYu0 more:网 ...
- 【bzoj3291】Alice与能源计划 模拟费用流+二分图最大匹配
题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...
- 【Bzoj3944】杜教筛模板(狄利克雷卷积搞杜教筛)
题目链接 哇杜教筛超炫的 有没有见过$O(n^\frac{2}{3})$求欧拉函数前缀和的算法?没有吧?蛤蛤蛤 首先我们来看狄利克雷卷积是什么 首先我们把定义域是整数,陪域是复数的函数叫做数论函数. ...
- BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
题目 现在,保密成为一个很重要也很困难的问题.如果没有做好,后果是严重的.比如,有个人没有自己去修电脑,又没有拆硬盘,后来的事大家都知道了. 当然,对保密最需求的当然是军方,其次才是像那个人.为了应付 ...
- 刷题总结——Bob's Race(hdu4123 树形dp+st表)
题目: Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the ro ...
- 爬虫【自动登陆github和抽屉】
自动登陆github用户详情页 代码 #! /usr/bin/env python # -*- coding: utf- -*- # __author__ = "wuxiaoyu" ...
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- button 默认类型是submit
“form表单里的按钮,明明是button而不是input type = submit,点击还是提交,如何让它不提交?” (这个问题被我遇到了,很坑爹的,知道了就不以为然了.) 因为 button 默 ...
- JavaScript 在双引号之间引用变量
可以采用 ' + 变量 + ' .