Python - Selenium Chrome 模拟手机
2017-04-26 16:59
746 查看
Python - Selenium Chrome 模拟手机
Max.Bai2017-04
Chrome浏览器支持移动端调试,当然ChromeDriver也支持移动端测试了。
使用python调用Webdriver 的Chrome浏览器驱动,并设置对应的options就可以实现浏览器打开后为移动端模式。
RobotFramework - Chrome模拟手机
1. 简单模式,指定对应的手机设备即可
mobile_emulation = {"deviceName":"Google Nexus 5"}完整代码
# -*- coding:utf-8 -*- import time from selenium import webdriver from selenium.webdriver.chrome.options import Options mobile_emulation = {"deviceName":"Google Nexus 5"} chrome_options = Options() chrome_options.add_experimental_option("mobileEmulation", mobile_emulation) driver = webdriver.Chrome(chrome_options = chrome_options) driver.get("http://www.baidu.com") time.sleep(5) driver.close()
2. 复杂模式,可以设置设备的宽度等等等
可包含deviceMetrics, userAgentmobile_emulation = { "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 }, "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" }
完整代码同简单模式,替换下就可以
3. Grid模式下创建webdriver
Grid是Selenium分布式执行模式(搭建分布式)和本地模式不同的是需要使用Remote方法。
实例代码:
# -*- coding:utf-8 -*- import time from selenium import webdriver mobile_emulation = { "deviceName": "Google Nexus 5" } chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("mobileEmulation", mobile_emulation) driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities = chrome_options.to_capabilities()) driver.get("http://www.baidu.com") time.sleep(5) driver.close()
最后附一张截图,看看浏览器打开是什么样子的
相关文档:
https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation
相关文章推荐
- python+chrome+Selenium模拟手机浏览器
- python+chrome+Selenium模拟手机浏览器
- python+chrome+Selenium模拟手机浏览器
- Python selenium —— 用chrome的Mobile emulation模拟手机浏览器测试手机网页
- python+Selenium2+chrome构建浏览器模拟环境
- 模拟页面输入:python + selenium + chrome
- Selenium-Webdriver(python) (五) --- 电脑模拟手机访问手机版网页
- 使用python通过selenium模拟打开chrome窗口报错 出现 "您使用的是不受支持的命令行标记:--ignore-certificate-errors
- python模拟打开Google浏览器并打开网页(selenium+chrome)
- python selenium 模拟手机浏览器
- Python selenium —— 用chrome的Mobile emulation模拟手机浏览器测试手机网页
- selenium启动chrome模拟器模拟手机
- selenium+python自动化84-chrome手机wap模式
- 使用python+Selenium2+chrome模拟用户登录
- python+Selenium2+chrome构建动态网页爬虫工具
- 用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!
- chrome模拟手机访问网站
- Chrome模拟手机访问页面
- 无需插件,直接加参数,chrome即可模拟手机浏览器
- chrome33下模拟手机显示页面(开启设备模拟器)