【问题标题】:pytest-selenium use mobile_emulation or add userAgent in settingpytest-selenium 使用 mobile_emulation 或在设置中添加 userAgent
【发布时间】:2017-04-27 04:22:42
【问题描述】:

在 Selenium 中,我们可以使用 mobileEmulation 将 chrome 驱动程序更改为移动模式

mobile_emulation = {
        "deviceMetrics": { "width": 360, "height": 640, "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)
    webdriver.Chrome(chrome_options = chrome_options)

我检查了 pytest-selenium 有功能选项,但不适用于用户代理

我有什么解决方案可以在 pytest-selenium 中模拟移动设备吗?

谢谢

【问题讨论】:

  • 这看起来像来自example 的代码。 “不工作”是什么意思?怎么了?你有错误吗?
  • 哦,我想使用 pytest-selenium 报告,所以我想将 useragent 传递给它,似乎不起作用,chrome 浏览器对 useragent 和指标无效
  • 恐怕我还不清楚。您能否准确展示您当前看到的内容,以及如果它正常工作您希望看到的内容?

标签: python selenium selenium-webdriver pytest


【解决方案1】:

临时解决方案是将代码放在 conftest.py 中

使用yield diver让pytest-selenium获取dirver信息。

@pytest.yield_fixture
def mobileSelenium(request):
    mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "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(chrome_options = chrome_options)
    request.node._driver = driver
    yield driver
    driver.quit()

【讨论】:

    猜你喜欢
    • 2017-03-12
    • 1970-01-01
    • 2014-06-08
    • 2021-08-05
    • 1970-01-01
    • 2017-07-26
    • 2019-03-22
    • 2018-12-23
    • 2018-11-30
    相关资源
    最近更新 更多