我已经使用set position来改变webdriver自动化中的浏览器窗口大小。 当我使用Chrome驱动程序2.28运行代码时,它会抛出异常
**Exception starting line:**
driver.manage().window().setPosition(new Point(pos_x, pos_y));
**Trace:**
**Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot get automation extension**
from unknown error: page could not be found: chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
**(Session info: chrome=57.0.2987.110)
(Driver info: chromedriver=2.28.455520** (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.21 seconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'TESTVM', ip: 'xx.xx.xx.xx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.28.455520 (cc17746adff54984afff480136733114c6b3704b), userDataDir=C:\Users\xxxxx\AppData\Local\Temp\scoped_dir21364_25403}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, **version=57.0.2987.110**, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 2af85eb51f31434f8df972a737b82bd4
实际使用案例是打开多个Chrome窗口并使所有窗口成为焦点。现在,我无法调整窗口大小。
最终的ChromeOptions选项=新的ChromeOptions(); final String adminToolURL = “http://www.google.com”; options.addArguments( “ - 禁用扩展”);
final WebDriver driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(500, 600));
driver.get(adminToolURL);
driver.getWindowHandles();
final String adminToolHandle = driver.getWindowHandle();
int x = 0, y = 30;
String window = "win";
for (int i = 1; i <= 3; i++) {
window = window + String.valueOf(i);
System.out.println(x);
System.out.println(y);
((JavascriptExecutor) driver)
.executeScript("window.open('https://www.google.com','"
+ window + "',height=" + x + ",width=" + y + ");");
final String handle = driver.getWindowHandle();
driver.switchTo().window(handle.toString());
// driver.manage().window().setPosition(new Point(pos_x, pos_y));
pos_x = pos_x + 180;
if ((i % 7) == 0) {
pos_y = pos_y + 160;
pos_x = 0;
}
x += 30;
y += 40;
Thread.sleep(2000);
final Set<String> customerWindow = driver.getWindowHandles();
customerWindow.removeAll(customerWindow);
driver.switchTo().window(adminToolHandle);
}
Thread.sleep(600000);
driver.quit();
}
答案 0 :(得分:0)
我也遇到过这个问题。
解决方案非常简单 - 您需要使用选项启动ChromeDriver。
var options = new ChromeOptions();
options.AddArgument("start-maximized");
_driver = new ChromeDriver(options);
代码在C#上。在jave上它必须几乎相同。
因此,这是一种解决方法,可以在不出错的情况下最大化浏览器。