。如果您的连接不安全,则身份验证弹出代码不适用于使用Java的Selenium
我使用机器人类和线程在应用程序中处理身份验证弹出窗口。
为处理此身份验证弹出窗口,我创建了单独的线程并使用机械手类,它可以正常工作并达到预期的结果。
但是在某些情况下,我的代码无法正常工作。
问题: 在我的应用程序提供的功能中,复制URL并粘贴到浏览器(Chrome)中,然后出现身份验证弹出窗口及其预期的结果。
一段时间后,URL需要花费一些时间才能加载,然后Chrome浏览器在弹出窗口出现之前显示通知。如果浏览器通知出现 代码不起作用。
请参阅屏幕截图和我的代码。
// Chrome浏览器的代码
>else if (CONFIG.getProperty("browserType").equals("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");
>// Enable Flash for this site
> 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);
>
> APP_LOGS.debug("Chrome Browser launch successfully");
> isBrowserOpened=true;
> String waitTime=CONFIG.getProperty("default_implicitWait");
> driver.manage().timeouts().implicitlyWait(Long.parseLong((waitTime).trim()),
> TimeUnit.SECONDS);
> driver.manage().timeouts().pageLoadTimeout(150, TimeUnit.SECONDS);
}
//内部类,用于处理身份验证弹出线程
公共类HandleAuthWindow实现Runnable {@Override public void run() { try { AuthWindow(); } catch (Exception ex) { System.out.println("Error in Login Thread: " + ex.getMessage()); } } public void AuthWindow() throws Exception { //wait - increase this wait period if required Thread.sleep(10000); //create robot for keyboard operations Robot rb = new Robot(); //Enter user name by ctrl-v StringSelection username = new StringSelection(CONFIG.getProperty("username")); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null); rb.keyPress(KeyEvent.VK_CONTROL); rb.keyPress(KeyEvent.VK_V); rb.keyRelease(KeyEvent.VK_V); rb.keyRelease(KeyEvent.VK_CONTROL); //tab to password entry field rb.keyPress(KeyEvent.VK_TAB); rb.keyRelease(KeyEvent.VK_TAB); Thread.sleep(2000); //Enter password by ctrl-v StringSelection pwd = new StringSelection(CONFIG.getProperty("password")); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null); rb.keyPress(KeyEvent.VK_CONTROL); rb.keyPress(KeyEvent.VK_V); rb.keyRelease(KeyEvent.VK_V); rb.keyRelease(KeyEvent.VK_CONTROL); //press enter rb.keyPress(KeyEvent.VK_ENTER); rb.keyRelease(KeyEvent.VK_ENTER); //wait Thread.sleep(3000); } }
//在我们的测试用例中调用此线程,请参见下文 //在当前浏览器中打开新标签,然后将焦点转移到新标签上
((JavascriptExecutor)driver).executeScript("window.open('about:blank','_blank')"); >Thread.sleep(1000); >ArrayList<String> tabs=new > ArrayList<String>(driver.getWindowHandles()); > > driver.switchTo().window(tabs.get(1)); > try { >//create new thread for interaction with windows authentication >(new Thread(new HandleAuthWindow())).start(); >//openning copyodta url > driver.get(DataLink); } catch (Exception e) { > System.out.println("Exception Found while handling Auth Popup window > and launching CopyOdata Url , "); ); }
感谢任何帮助 谢谢
答案 0 :(得分:0)
您可以使用以下代码禁用chrome的通知:
ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "path to driver");
WebDriver driver = new ChromeDriver(ops);
对于Firefox,您可以尝试以下方法:
System.setProperty("webdriver.gecko.driver","path to driver");
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("dom.webnotifications.enabled", false);
WebDriver driver = new FirefoxDriver(ffprofile);
已弃用,但仍然可以尝试解决。
供参考:https://superuser.com/questions/959269/disabling-the-enable-notifications-popup-in-firefox
您可以手动执行。
希望它对您有帮助。