在 selenium 中使用移动浏览器而不模拟设备? [英] Use mobile browser in selenium without emulating the device?

查看:20
本文介绍了在 selenium 中使用移动浏览器而不模拟设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 selenium 在 python 中编写一个脚本,它应该自动将图像上传到图像平台.但是,问题是,上传图片的功能仅在您使用移动浏览器(例如 iPhone 上的 Safari)时才提供.在快速研究中,我发现 selenium 支持此功能,但据我所知,只有在您模拟设备或在计算机上连接真实设备时才会提供此功能.如果您想使用 python 模拟移动浏览器,是否有另一种方法(甚至可能是另一个库?)没有这样的开销(连接或模拟设备)?

I tried to write a script in python using selenium, that should automate uploading images to an image platform. However, the problem is, that the functionality for uploading pictures is only given, when you use a mobile browser (e.g. Safari on your iPhone). In a quick research, i found that selenium supports this, but as far as i understood it, this is only given if you emulate the device or connect a real device on your computer. Is there another way (maybe even another library?) for not having such an overhead (connecting or emulating the device), if you want to simulate an mobile browser using python?

推荐答案

传递正确的用户代理应该可以解决问题.使用移动 Chrome 的示例:

Passing correct user agent should do the trick. Example with mobile Chrome:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://www.google.com')

这篇关于在 selenium 中使用移动浏览器而不模拟设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆