失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Selenium之Chrome模拟手机浏览器

Selenium之Chrome模拟手机浏览器

时间:2020-02-29 03:51:28

相关推荐

Selenium之Chrome模拟手机浏览器

1. 第一种方法

第一种方法是通过device name来指定要模拟的手机

from selenium import webdrivermobileEmulation = {'deviceName': 'iPhone X'}options = webdriver.ChromeOptions()options.add_experimental_option('mobileEmulation', mobileEmulation)driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)driver.get('')driver.close()

1. 第二种方法

第二种方法是通过分辨率以及UA标识指定要模拟的手机

from selenium import webdriverWIDTH = 320HEIGHT = 640PIXEL_RATIO = 3.0UA = 'Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/6.3'mobileEmulation = {"deviceMetrics": {"width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO}, "userAgent": UA}options = webdriver.ChromeOptions()options.add_experimental_option('mobileEmulation', mobileEmulation)driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)driver.get('')driver.close()

如果觉得《Selenium之Chrome模拟手机浏览器》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。