无法在远程计算机上最大化Chrome浏览器

时间:2018-06-23 06:36:32

标签: java selenium selenium-webdriver selenium-chromedriver

我无法使用Selenium来最大化chrome窗口。

System.setProperty("webdriver.chrome.driver",CONFIG.getProperty("driverPath")+"chromedriver.exe");          
String downloadFilepath = filePath;
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups",0);
//1-Allow, 2-Block, 0-default             
chromePrefs.put("profile.default_content_setting_values.notifications", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
chromePrefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);           
chromePrefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);
chromePrefs.put("PluginsAllowedForUrls", CONFIG.getProperty("websiteUrl"));

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>()
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");                   
options.addArguments("--disable-notifications");
options.addArguments("disable-infobars");
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--test-type");
options.addArguments("--start-maximized"); DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);            
cap.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);             
cap.setCapability(ChromeOptions.CAPABILITY, options);   
driver = new ChromeDriver(cap);

请参阅附件screenshot 任何帮助表示赞赏 预先感谢

3 个答案:

答案 0 :(得分:0)

替换此代码:

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);            
cap.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);              cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);               cap.setCapability(ChromeOptions.CAPABILITY, options);    
driver = new ChromeDriver(cap);

对此:

options.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);            
options.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);            
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);             
options.setCapability(ChromeOptions.CAPABILITY, options);    
driver = new ChromeDriver(options);

答案 1 :(得分:0)

我不知道您是否尝试过此方法,但是如果不尝试,请尝试一下:

driver.manage().window().maximize(); 

driver.manage().window().setSize(new Dimension(1366, 768));  

这里1366是远程机器显示屏的宽度和768高。

希望有帮助。

答案 2 :(得分:0)

如果其他任何操作失败,则总会有JavaScriptExecutor。您可以使用

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.resizeTo(1366, 768);");

其中1366,768是您要最大化的尺寸或屏幕尺寸。