chrome浏览器模拟打开手机图

第一种方法(已存在的主流设备)

  1. # -*- coding: utf-8 -*-
  2. from selenium import webdriver
  3. from time import sleep
  4. #{'deviceName': '必须与谷歌浏览器的值一致'}
  5. mobileEmulation = {'deviceName': 'iPhone 6/7/8'}
  6. options = webdriver.ChromeOptions()
  7. options.add_experimental_option('mobileEmulation', mobileEmulation)
  8. driver = webdriver.Chrome( chrome_options=options)
  9. driver.get('http://m.baidu.com')
  10. sleep(3)
  11. driver.close()

第二种方法(自定义长宽)

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. from selenium import webdriver
  4. from selenium.webdriver.chrome.options import Options
  5. mobile_emulation = {
  6. "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0}, # 定义设备高宽,像素比
  7. "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) " # 通过UA来模拟
  8. "AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"}
  9. chrome_options = Options()
  10. chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
  11. driver = webdriver.Chrome(chrome_options = chrome_options)
  12. driver.get("http://m.baidu.com")

常用deviceName列表

  1. mobile_emulation = {
  2. "deviceName": "Apple iPhone 3GS",
  3. "deviceName": "Apple iPhone 4",
  4. "deviceName": "Apple iPhone 5",
  5. "deviceName": "Apple iPhone 6",
  6. "deviceName": "Apple iPhone 6 Plus",
  7. "deviceName": "BlackBerry Z10",
  8. "deviceName": "BlackBerry Z30",
  9. "deviceName": "Google Nexus 4",
  10. "deviceName": "Google Nexus 5",
  11. "deviceName": "Google Nexus S",
  12. "deviceName": "HTC Evo, Touch HD, Desire HD, Desire",
  13. "deviceName": "HTC One X, EVO LTE",
  14. "deviceName": "HTC Sensation, Evo 3D",
  15. "deviceName": "LG Optimus 2X, Optimus 3D, Optimus Black",
  16. "deviceName": "LG Optimus G",
  17. "deviceName": "LG Optimus LTE, Optimus 4X HD" ,
  18. "deviceName": "LG Optimus One",
  19. "deviceName": "Motorola Defy, Droid, Droid X, Milestone",
  20. "deviceName": "Motorola Droid 3, Droid 4, Droid Razr, Atrix 4G, Atrix 2",
  21. "deviceName": "Motorola Droid Razr HD",
  22. "deviceName": "Nokia C5, C6, C7, N97, N8, X7",
  23. "deviceName": "Nokia Lumia 7X0, Lumia 8XX, Lumia 900, N800, N810, N900",
  24. "deviceName": "Samsung Galaxy Note 3",
  25. "deviceName": "Samsung Galaxy Note II",
  26. "deviceName": "Samsung Galaxy Note",
  27. "deviceName": "Samsung Galaxy S III, Galaxy Nexus",
  28. "deviceName": "Samsung Galaxy S, S II, W",
  29. "deviceName": "Samsung Galaxy S4",
  30. "deviceName": "Sony Xperia S, Ion",
  31. "deviceName": "Sony Xperia Sola, U",
  32. "deviceName": "Sony Xperia Z, Z1",
  33. "deviceName": "Amazon Kindle Fire HDX 7″",
  34. "deviceName": "Amazon Kindle Fire HDX 8.9″",
  35. "deviceName": "Amazon Kindle Fire (First Generation)",
  36. "deviceName": "Apple iPad 1 / 2 / iPad Mini",
  37. "deviceName": "Apple iPad 3 / 4",
  38. "deviceName": "BlackBerry PlayBook",
  39. "deviceName": "Google Nexus 10",
  40. "deviceName": "Google Nexus 7 2",
  41. "deviceName": "Google Nexus 7",
  42. "deviceName": "Motorola Xoom, Xyboard",
  43. "deviceName": "Samsung Galaxy Tab 7.7, 8.9, 10.1",
  44. "deviceName": "Samsung Galaxy Tab",
  45. "deviceName": "Notebook with touch",
  46. "deviceName": "iPhone 6"
  47. }