Change the properties of the PC-side Chrome browser and set the PC-side Chrome browser to a mobile-phone-size browser.
#!usr/bin/env python#-*-coding:utf-8-*-#edit chrome settings disguised as mobile m station fromSeleniumImportWebdriverImportUnittest,timeclassTestdemo (unittest. TestCase):defTest_ipadchrome (self): Options=Webdriver. Chromeoptions ()#--user-agent= ' xxxx ' to modify the HTTP request header's agent string to disguise the PC-side Chrome browser as a mobile browserOptions.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') Driver= Webdriver. Chrome (chrome_options=options) Driver.get ('http://www.baidu.com') Time.sleep (3) driver.find_element_by_id ('kw'). Send_keys ('IPad') Time.sleep (2) #View Camouflage EffectsDriver.get ('about:version') Time.sleep (10) Driver.quit ()defTest_iphonechrome (self): Options=Webdriver. Chromeoptions () options.add_argument ('--user-agent=mozilla/5.0 (IPhone; CPU iPhone 5_0 like Mac OS X) applewebkit/534.46 (khtml,like Gecko) version/5.1 mobile/9a334 safari/7534.48. 3') Driver= Webdriver. Chrome (chrome_options=options) Driver.get ('http://www.baidu.com') Time.sleep (2) driver.find_element_by_id ('index-kw'). Send_keys ('IPhone') Time.sleep (2) Driver.get ('about:version') Time.sleep (10) Driver.quit ()defTest_android236chrome (self): Options=Webdriver. Chromeoptions () options.add_argument ('--user-agent=mozilla/5.0 (Linux; U Android 2.3.6;en-us; Nexus S build/grk39f) applewebkit/533.1 (khtml,like Gecko) version/4.0 Mobile safari/533.1') Driver= Webdriver. Chrome (chrome_options=options) Driver.get ('http://www.baidu.com') Time.sleep (2) driver.find_element_by_id ('index-kw'). Send_keys ('Android 2.3.6') Time.sleep (2) Driver.get ('about:version') Time.sleep (10) Driver.quit ()defTest_android402chrome (self): 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') Driver= Webdriver. Chrome (chrome_options=options) Driver.get ('http://www.baidu.com') Time.sleep (2) driver.find_element_by_id ('index-kw'). Send_keys ('Android 4.0.2') Time.sleep (2) Driver.get ('about:version') Time.sleep (10) Driver.quit ()if __name__=='__main__': Unittest.main ()
Edit Chrome settings disguised as mobile M station