浏览器模拟手机模式操作

代码实现如下:

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('李白
')
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('李白
')
driver.quit()
原文地址:https://www.cnblogs.com/zhuxibo/p/14266540.html