簡體   English   中英

在Chrome瀏覽器中。 如果您的連接不安全,則身份驗證彈出代碼不適用於使用Java的Selenium。

[英]in Chrome browser. authentication popup code not working with Selenium using Java if your connection is not secure notification appears

在Chrome瀏覽器中。 如果您的連接不安全,則身份驗證彈出代碼不適用於使用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 , "); ); } 

任何幫助表示贊賞,謝謝

您可以使用以下代碼來禁用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
您可以手動執行。

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM