【问题标题】:Python and Selenium mobile emulationPython 和 Selenium 移动仿真
【发布时间】:2020-08-24 03:00:03
【问题描述】:

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

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

我的代码有什么遗漏或错误?

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver emulation


    【解决方案1】:

    试试这个。

    iphoneX [width:375, height:812, pixelRatio:3].

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    mobile_emulation = {
        "deviceMetrics": { "width": 375, "height": 812, "pixelRatio": 3.0 },
        "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
    }
    
    chrome_options = Options()
    chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
    driver = webdriver.Chrome(
        executable_path="../chrome/chromedriver85", options=chrome_options
    )
    
    url = "https://google.com/"
    driver.get(url)
    

    【讨论】:

      【解决方案2】:

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

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

      如果此答案有帮助/有效,请标记为有效,即使您已经有了解决方案。仅供以下程序员使用:)

      贝尼。

      【讨论】:

        【解决方案3】:

        您需要编写 iPhone X,而您编写的 iphone X 应该可以修复它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-21
          • 1970-01-01
          • 1970-01-01
          • 2014-05-08
          • 2011-08-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多