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中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...
随机推荐
- adb pull 文件夹的时候注意
传说之美 分享快乐 记录生活 学习探索 博客园 首页 新随笔 联系 管理 订阅 随笔- 75 文章- 0 评论- 19 Android 用adb pull或push 拷贝手机文件到到电脑上,拷贝 ...
- Model View Controller(MVC) in PHP
The model view controller pattern is the most used pattern for today’s world web applications. It ha ...
- http.server()的理解
http.server()相当于实例化一个server,等价于http.createServer(). 以下为个人理解 http.server()为创建一个http服务,http.server()可以 ...
- Linux基础之Linux简介
Linux(英语发音:/ˈlɪnəks/ lin-əks)是一种自由和开放源代码的类UNIX操作系统. Linux简介 Linux(英语发音:/ˈlɪnəks/ lin-əks)是一种自由和开放源代码 ...
- iOS--app自定义相册--创建相簿,存储图片到手机
我们在APP中点击照片,都会显示出大图,然后在大图的上面会有个保存照片的按钮,照片直接保存到了系统的相册中,但是因为公司产品的需要,让你创建和APP同名的相册保存在里面,那么就对了,可以看下具体的代码 ...
- 爬虫【自动登陆github和抽屉】
自动登陆github用户详情页 代码 #! /usr/bin/env python # -*- coding: utf- -*- # __author__ = "wuxiaoyu" ...
- 标准C程序设计七---107
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 致命错误:ext/standard/php_smart_str.h:没有那个文件或目录
致命错误:ext/standard/php_smart_str.h:没有那个文件或目录 参考文章:https://blog.csdn.net/jartins/article/details/80371 ...
- AC日记——Propagating tree Codeforces 383c
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 基于python的性能测试工具–locust
现在有很多的性能测试工具,比如说我们熟悉的loadrunner.jmeter.ab.webbench等等,这些工具如果对一个没用过的朋友来说,学习起来比较不容易,但是如果你能看懂python代码,会写 ...