selenium模拟不同浏览器的方式

from selenium import webdriver

# 使用Firefox手机浏览器
user_agent = "Mozilla/5.0 (iPad; CPU OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", user_agent)
driver = webdriver.Firefox(profile, executable_path="geckodriver")
# 设置窗口大小
driver.set_window_size(1080, 1920)
testUrl = 'www.baidu.com'  # 已替换
# 加载指定的网址
driver.get(testUrl)

# 模拟chrome手机浏览器
mobileEmulation = {'deviceName': 'iPad'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
testUrl = 'www.baidu.com'  # 已替换
driver.get(testUrl)
# 设置窗口大小
driver.set_window_size(1024, 1366)

###
实现逻辑
###

 

posted @ 2019-09-02 11:14  小学弟-  阅读(609)  评论(0编辑  收藏  举报