首页 > 移动平台 > 详细

浏览器模拟手机模式操作

时间:2021-01-12 16:34:19      阅读:36      评论:0      收藏:0      [点我收藏+]

代码实现如下:

import os
from  selenium import webdriver

#配置浏览器以手机模式启动
chrome_options = webdriver.ChromeOptions()
#选择一种存在的模式手机设备(分辨率)
chrome_options.add_experimental_option(‘mobileEmulation‘,{‘deviceName‘:‘iPhone X‘})
print(type(chrome_options)) #import os
from  selenium import webdriver

#配置浏览器以手机模式启动
chrome_options = webdriver.ChromeOptions()
#选择一种存在的模式手机设备(分辨率)
chrome_options.add_experimental_option(‘mobileEmulation‘,{‘deviceName‘:‘iPhone X‘})
print(type(chrome_options)) #<class ‘selenium.webdriver.chrome.options.Options‘>
print(chrome_options.to_capabilities()) #{‘browserName‘: ‘chrome‘, ‘version‘: ‘‘, ‘platform‘: ‘ANY‘, ‘goog:chromeOptions‘: {‘mobileEmulation‘: {‘deviceName‘: ‘iPhone X‘}, ‘extensions‘: [], ‘args‘: []}}
#将chrome_options:创建一个包含所有已设置选项和返回包含所有内容的词典
new_new_chrome_options = chrome_options.to_capabilities()
path= os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
executable_path = f‘{path}/driver/chromedriver.exe‘
#创建一个driver
driver = webdriver.Chrome(executable_path=executable_path,desired_capabilities=new_new_chrome_options)
driver.get(‘http://www.baidu.com‘)
driver.find_element_by_id(‘index-kw‘).send_keys(‘李白\n‘)
driver.quit()
print(chrome_options.to_capabilities())
#将chrome_options:创建一个包含所有已设置选项和返回包含所有内容的词典
new_new_chrome_options = chrome_options.to_capabilities()
path= os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
executable_path = f‘{path}/driver/chromedriver.exe‘
#创建一个driver
driver = webdriver.Chrome(executable_path=executable_path,desired_capabilities=new_new_chrome_options)
driver.get(‘http://www.baidu.com‘)
driver.find_element_by_id(‘index-kw‘).send_keys(‘李白\n‘)
driver.quit()

 

浏览器模拟手机模式操作

原文:https://www.cnblogs.com/zhuxibo/p/14266540.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!