1. 程式人生 > >chrome模擬手機瀏覽器python+selenium

chrome模擬手機瀏覽器python+selenium

python

近段時間測試H5頁面,使用chrome瀏覽器然後F12切換到手機模式;剛開始使用selenium框架然後再模擬操作F12,但是發現切換到手機模式後又恢復到瀏覽器的原始模式了,後來就各種百度,終於找到答案了,以下是示例代碼 # -*- coding: utf-8 -*- from selenium import webdriver from time import sleep protocol='http' url=protocol+'://m.baidu.com' mobileEmulation = {'deviceName': 'iPhone 6'} options = webdriver.ChromeOptions() options.add_experimental_option('mobileEmulation', mobileEmulation) driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options) driver.get(url) class OpenH5(): def openBaidu(self): #driver=self.driver print "點擊輸入框" driver.find_element_by_id("index-kw").clear() driver.find_element_by_id("index-kw").send_keys(u"測試") driver.find_element_by_id("index-bn").click() driver.get_screenshot_as_file("../../screen/chaweizhang.png") sleep(3) #driver.find_element_by_xpath(".//*[@id='app']/div/div/div[3]/ul/li[3]/span[text()='活動']").click() print u'關閉瀏覽器' driver.quit() login1 =OpenH5() login1.openBaidu() sleep(5)


chrome模擬手機瀏覽器python+selenium