selenium实现Chrome伪装成M站,模拟手机端浏览器,并完成页面操作
原理:
通过--user-agent="xxx"
来修改HTTP请求头部的Agent字符串,设置个人代理,使得PC端的Chrome浏览器伪装成手机浏览器,可以在地址栏输入”about:version”查看修改效果。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #!/usr/bin/env python3 # -*- coding: utf-8 -*- from selenium import webdriver from Log import * import time
class TestMobile(): """测试chrome伪装成手机M站"""
def test_iPadChrome(self): # 添加chrome设置 info(u"===========伪装成ipad==========") info(u"=====开始设置ipad个人代理=====") options = webdriver.ChromeOptions() options.add_argument( '--user-agent=Mozilla/5.0(iPad; CPU OS 5_0 like Mac OS X)' 'AppleWebKit/534.46(KHTML, like Gecko) Version/5.1' 'Mobile/9A334 Safari/7534.48.3' ) browser = webdriver.Chrome(chrome_options=options) browser.get("https://www.baidu.com") time.sleep(3) browser.find_element_by_id('kw').send_keys("iPad") time.sleep(1) # 通过在Chrome浏览器地址栏输入about:version,查看伪装效果 browser.get("about:version") # 人工确认“用户代理”项配置信息是否和设置一致 time.sleep(10) info(u"==========伪装ipad成功==========") browser.quit()
def test_iPhoneChrome(self): # 添加chrome配置 info(u"==========测伪装成iphone==========") info(u"=====设置iphone个人代理=====") options = webdriver.ChromeOptions() options.add_argument( '--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X)' 'AppleWebKit/534.46(KHTML, like Gecko) Version/5.1' 'Mobile/9A334 Safari/7534.48.3' ) browser = webdriver.Chrome(chrome_options=options) browser.get("https://www.baidu.com") time.sleep(3) # 定位搜索框 browser.find_element_by_id("index-kw").send_keys("iPhone") time.sleep(1) browser.get("about:version") time.sleep(10) info(u"==========伪装成iphone成功==========") browser.quit()
def testAndroid236Chrome(self): info(u"==========测试伪装成android2.3.6==========") info(u"=====设置android2.3.6个人代理=====") options = webdriver.ChromeOptions() options.add_argument( '--user-agent=Mozilla/5.0 (Linux; U; Android 2.3.5; en-us)' 'Nexus S Build/GRK39F) AppleWebKit/533.1' '(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' ) brower = webdriver.Chrome(chrome_options=options) brower.get("https://www.baidu.com") # 定位搜索输入框 brower.find_element_by_id("index-kw").send_keys("Android 2.3.6") time.sleep(1) brower.get("about:version") time.sleep(10) info(u"==========伪装成android2.3.6成功==========") brower.quit()
def testAndroid402Chrome(self): info(u"==========测试伪装成android4.0.2==========") info(u"=====设置android4.0.2个人代理") options = webdriver.ChromeOptions() options.add_argument( '--user-agent=Mozilla/5.0 (Linux; U; Android 4.0.2;' 'en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30' '(KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' ) browser = webdriver.Chrome(chrome_options=options) browser.get("http://www.baidu.com") time.sleep(3) browser.find_element_by_id("index-kw").send_keys("Android 4.0.2") time.sleep(1) browser.get("about:version") time.sleep(10) info(u"==========伪装成android4.0.2成功==========") browser.quit()
if __name__ == '__main__': testmobile = TestMobile() testmobile.testAndroid402Chrome() testmobile.testAndroid236Chrome() testmobile.test_iPhoneChrome() testmobile.test_iPadChrome()
|
偶然看见这个实现,目前没想到应用场景。。直接使用appium进行移动自动化好像也能实现啊。。(可能工作中遇见的太少了)