我正在尝试使用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/