Python和Selenium移动仿真

标签 python selenium selenium-webdriver selenium-chromedriver emulation

我正在尝试使用Selenium仿真和Python仿真iPhone X的Chrome,如下所示:

from selenium import webdriver

mobile_emulation = { "deviceName": "iphone X" }

chrome_options = webdriver.ChromeOptions()

chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(r'C:\Users\Alex\PythonDev\chromedriver')

driver.get('https://www.google.com')

但是,没有任何 react :我的页面仍然是普通的浏览器页面,并且我不认为它是移动页面。

我的代码中缺少什么或有什么错误?

最佳答案

您可能现在已经找到了答案,但这是一个简单的答案:
在您的代码示例中,驱动程序没有机会知道您希望它模拟其他设备。
这是完整的工作代码:

from selenium import webdriver
mobile_emulation = { "deviceName": "your device" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(options=chrome_options) #sometimes you have to insert your execution path
driver.get('https://www.google.com')
确保Chrome支持您的设备,并且您的设备名称拼写正确。
如果该答案是有帮助的/可行的,即使您已经有解决方案,也请标记为这样。仅适用于以下程序员:)
贝尼

关于Python和Selenium移动仿真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61681097/

相关文章:

python - imshow奇怪的cv2行为

python - 从字符串创建 Python 对象

java - 在 Selenium Grid 中注册 PhantomJS 节点时出错

java - 新安装时出现 nullpointerException 错误

java - Selenium - 如何点击这个跨度类

php - 对象存储的创建方法

python - 是否需要在 Cython 中显式删除动态分配的容器(如 std::vector)

python - 删除由 selenium/chromedriver 传递给 chrome 的参数

java - 如何使用 Java 处理与 Selenium 自动化中的其他元素重叠的日历?

java - 如何为不同行具有不同列号的 Excel 工作表编写 TestNG DataProvider 注释