谷歌浏览器模拟手机浏览器且指定IP运行
1.背景
因为现在项目是要做分布式,而以前使用谷歌浏览器模拟手机运行做的分布式,是指定在某台机器运行是通过Jenkins配置,来指定服务器,但是这样有一个问题,如果大家都同时配置到某台电脑,那台服务器压力就很大,所以需要根据每台服务器的情况,去分配任务,那我就需要解决第一个问题,如何让模拟器指定ip运行,我的项目的部署方式(分布式)使用的selenium grid 的方式,使用模拟器方式最开始一直使用的是ChromeDriver方式启动,但是这个ChromeDriver方式启动只能是启动本地浏览器,进行手机模拟,不能指定在某台IP上进行运行
最开始看了很多官网资料,https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation ,针对ChromeDriver java没有办法指定IP,而python可以,最开始走了一大圈,想通过ChromeDriver的方法来进行改变,饶了一大圈的弯路,最后想通了一点只要更换启动方式就可以了

以前启动手机模拟代码是这样:
public void initdriver(){
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-infobars");
Map<String, Object> prefs = new HashMap<String, Object>();
// 是否加载图片
// prefs.put("profile.managed_default_content_settings.images", 2);
options.setExperimentalOption("prefs", prefs);
Map<String, Object> deviceMetrics = new HashMap<String, Object>();
deviceMetrics.put("width", 360);
deviceMetrics.put("height", 640);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent",
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe");
driver = new ChromeDriver(options);
}
而现在我只需要把启动方式做更改
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-infobars");
Map<String, Object> prefs = new HashMap<String, Object>();
// 是否加载图片
// prefs.put("profile.managed_default_content_settings.images", 2);
options.setExperimentalOption("prefs", prefs);
Map<String, Object> deviceMetrics = new HashMap<String, Object>();
deviceMetrics.put("width", 360);
deviceMetrics.put("height", 640);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent",
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe");
if(TestngListener2.getProjectCode().contains("platform")){//这里表示使用的平台,为了不影响以前框架使用
System.out.println("使用的平台进行启动的浏览器");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
try {
driver = new RemoteWebDriver(new URL("http://10.40.2.114:5555/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}else{
driver = new ChromeDriver(options);
} }
谷歌浏览器模拟手机浏览器且指定IP运行的更多相关文章
- 在PC上测试移动端网站和模拟手机浏览器的5大方法
在PC上测试移动端网站和模拟手机浏览器的5大方法 来源:互联网 作者:佚名 时间:03-19 10:14:54 [大 中 小] 最近公司要开发网站的移动版,让我准备准备知 ...
- Selenium操作Chrome模拟手机浏览器
目录 使用指定设备 使用自定义设备 在使用Chrome浏览网页时,我们可以使用Chrome开发者工具模拟手机浏览器,在使用Selenium操作Chrome时同样也可以模拟手机浏览器.主要有以下两种用途 ...
- 用PC浏览器模拟手机浏览器(一):无扩展版
想浏览手机版,打开对应网址却跳转到PC版?怎么办? 下面咱们来说下在只是安装了浏览器,无需其他安装操作的情况下来怎么用PC浏览器模拟手机浏览器,然后访问手机站点. 浏览器众多,IE系列的咱就不考虑了, ...
- Opera浏览器测试移动端网站和模拟手机浏览器的方法
链接地址:http://www.neirong.org/post-256.html?utm_source=tuicool Chrome浏览器请看:Chrome浏览器测试移动端网站和模拟手机浏览器的方法 ...
- 无插件,直接加参数,chrome它可以模拟手机浏览器
在目标出现,加上一些参数即可:--user-agent="mozilla/5.0 (linux; u; android 2.3.3; en-us; sdk build/ gri34) app ...
- python自动化--mock、webservice及webdriver模拟手机浏览器
一.mock实现 自定义一个类,用来模拟未完成部分的开发代码 class Say(): def say_hello(self): pass 自定义返回值 import unittest from un ...
- 在PC上测试移动端网站和模拟手机浏览器的5大方
查了很多资料,尝试了大部分方法,下面将这一天的努力总结下分享给大家,也让大家免去看那么多文章,以下介绍的方法,都是本人亲自测试成功的方法,测试环境winxp. 一.Chrome*浏览器 chrome模 ...
- 【转】在PC上测试移动端网站和模拟手机浏览器的5大方法
查了很多资料,尝试了大部分方法,下面将这一天的努力总结下分享给大家,也让大家免去看那么多文章,以下介绍的方法,都是本人亲自测试成功的方法,测试环境winxp. 一.Chrome*浏览器 chrome模 ...
- PC上测试移动端网站和模拟手机浏览器
一.Chrome*浏览器 chrome模拟手机总共有四种方法,原理都一样,通过伪装User-Agent,将浏览器模拟成Android设备.以下标星的为推荐方法. 1.新建Chrome快捷方式 右击桌面 ...
随机推荐
- LeeCode(5. Longest Palindromic Substring)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Python Pandas -- Series
pandas.Series class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath ...
- IDEA修改git账号及密码的方法
IDEA修改git账号及密码的方法: 1.file->settings->passwords 2.重启IDEA 3.执行一次提交或更新 当执行提交或更新之后,idea会自动提示输入账号.密 ...
- 可视化开发_AppInventor2似乎被抛弃了
工具 blockly google,mixly,scratch,app inventor2 的分别 可视化编程,青雀,来自 白鹭 没源码 如果想二次开发呢,初版拖拽控件生成,后期维护的时候找程序员加功 ...
- goodBye wordPress
2016-07-28,我自己在GoDaddy上面注册了一个自己的域名,www.codetree.tech,同时在老薛主机上面购买了一个主机域名.我搭建了一个属于自己的博客,开心了很久.最近收到了域名续 ...
- 以多进程读取oss符合条件的数据为例,综合使用多进程间的通信、获取多进程的数据
import datetime import sys import oss2 from itertools import islice import pandas as pd import re im ...
- bootstrap-datepicker控件中文 ,只显示年和只显示月份
插件下载地址 https://github.com/uxsolutions/bootstrap-datepicker html <link href="css/bootstrap-da ...
- 11073 最热门的K个搜索串
11073 最热门的K个搜索串时间限制:350MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VCDescription大家都非常喜欢而习惯用baidu ...
- Host 'XXX' is not allowed to connect to this MySQL server解决方案
如何允许远程连接mysql数据库呢,操作如下: 首先登录账号 mysql -uroot -p 使用mysql用户 use mysql 如果报此类错:ERROR 1820 (HY000): You mu ...
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
最近在本地搭建的LNMP的开发环境.为了开发的时候不影响前端的正常开发就屏蔽的PHP里面php.ini中的一些错误提示.但是这样一来,就影响到了后端开发的一些问题比如不能及时调试开发中的一些问题 ng ...