【问题标题】:Need suggestions on how to change height and width using desired capabilities in chromedriver需要有关如何使用 chromedriver 中所需功能更改高度和宽度的建议
【发布时间】:2016-07-16 06:22:29
【问题描述】:

我正在使用具有 chrome 所需功能的 selenium webdriver 在模拟模式下在桌面浏览器上工作。我需要在我的代码的某些行之后更改高度和宽度属性。下面是我用来启动模拟 chrome 浏览器的摘录。但是如何在不重新启动浏览器的情况下更改高度和宽度属性。

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe"); 

Map<String, Object> deviceMetrics = new HashMap<String, Object>();
deviceMetrics.put("width", 900);
deviceMetrics.put("height", 700);
deviceMetrics.put("pixelRatio", 4.0);

Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like   Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53");

Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

driver = new ChromeDriver(capabilities);

我需要将宽度更改为 700,高度更改为 900。请有任何建议。

【问题讨论】:

    标签: selenium-webdriver google-chrome-devtools selenium-chromedriver emulation


    【解决方案1】:

    编辑:我最初的回答是,您只需要:

    driver.manage().window().setSize( new Dimension(700, 900) );
    

    (这是 WebDriver 规范中方法的 full details。如果您有一个打开的窗口,您可以随时调用它。)

    但是,这根本不起作用。它只是调整整个浏览器框架的大小,这是无用的,因为 Chrome 使用原始的移动仿真尺寸来创建 自己的内部框架,无法使用 WebDriver 方法调整其大小。 (这很公平,因为这是 Chrome 特有的功能。)

    例如,当我运行原始附加代码时,窗口大小原来是 (1247, 1319),因此将其大小调整为 (700,900) 只会减小整体框架大小,导致内框架滚动。

    我没有找到任何其他关于在运行期间修改 Chrome 选项的信息,所以我只能得出结论,这不可能

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      相关资源
      最近更新 更多