【发布时间】:2023-10-30 09:08:02
【问题描述】:
我在使用 chrome 和 selenium 2 webdriver 时遇到问题。问题是,当我使用 chromedriver 启动浏览器会话时,它总是在一个小于可用屏幕宽度一半大小的窗口中启动。因为我正在对屏幕尺寸更改时更改的页面进行测试,所以我的测试失败了,因为我试图从页面顶部拖动一个元素并将其放到页面底部的一个元素中。我收到滚动错误。但是如果窗口最大化,那么我不会得到这个错误。但问题是,每次 chrome 通过 chrome 驱动程序启动一个新会话时,它总是在一个小窗口中启动。我探索了许多不同的选项来让浏览器开始最大化:
- 通过 javascript 调用 window.resizeTo(width,height); 来更改窗口大小。但是,这不适用于更高版本的 chrome(或 firefox)。
- 使用开关 --start-maximized 启动 chrome。这适用于 Windows,但在 mac 上不起作用。
- 使用指定配置文件目录的开关启动 chrome。这样配置文件将定义窗口大小。 --profile-directory=~/chromeprofile 这在我从命令行启动 chrome 时有效,但如果我从 selenium webdriver/chromedriver 执行此操作,则没有任何区别。
-
当我尝试时:driver.manage().window().setSize() 我得到了这个例外:
INFO: Executing: [93debf43cf70ad3557442a7e1aee4620, setWindowSize {"windowHandle":"current","width":2560,"height":1440}] org.openqa.selenium.UnsupportedCommandException: [GET, HEAD, DELETE] Command duration or timeout: 16 milliseconds Build info: version: '2.15.0', revision: '15105', time: '2011-12-08 09:56:25' System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.2', java.version: '1.6.0_29' Driver info: driver.version: RemoteWebDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:424) at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.setSize(RemoteWebDriver.java:578) at com.domo.automation.framework.utility.WebDriverUtil.startWebDriver(WebDriverUtil.java:36) at com.domo.automation.tests.DomoWebDriverTestCase.setUp(DomoWebDriverTestCase.java:45) at junit.framework.TestCase.runBare(TestCase.java:132) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
为了实现这一目标,我还可以探索哪些其他选项?问题是窗口太小?每次 chrome 在 mac 上通过 webdriver 启动时,我该怎么做才能自动最大化窗口?
【问题讨论】: