Python和Selenium移动仿真 [英] Python and Selenium mobile emulation

查看:96
本文介绍了Python和Selenium移动仿真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to emulate Chrome for iPhone X with Selenium emulation and Python, as follow:

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')

但是,什么都没发生:我的页面仍然是普通的浏览器页面,并且我不认为它是移动页面.

However, nothing happens: my page is still a normal browser page, and I don't see it as a mobile page.

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

What is missing or wrong in my code?

推荐答案

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

You might have found an answer by now, but here's a general one: In your code example, your driver has no chance to know that you want it to emulate another device. Here's full working code:

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支持您的设备,并且您的设备名称拼写正确.

Make sure that Chrome supports your device and your device name is spelled correctly.

如果此答案是有帮助的/有效的,即使您已找到解决方案,也请标记为.它仅适用于以下程序员:)

If this answer was helpful/worked, please mark it as so, even if you already have a solution. It's just for following programmers :)

贝尼.

这篇关于Python和Selenium移动仿真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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