1

我使用的是selenium2(selenium-java:3.0.1)和phantomjs-2.1.1-linux-x86_64。我想要做的是進入需要SSO的頁面。使用瀏覽器訪問網站時,會彈出登錄對話框輸入用戶名和密碼。如何使用selenium2 + phantomjs webdriver傳遞SSO?

使用wget獲取URL時。它停在auth部分。

[email protected]:wget https://www.example.com/details 
--2016-11-15 05:18:02-- https://www.example.com/details 
Resolving www.example.com (www.example.com)... 10.20.30.40 
Connecting to www.example.com (www.example.com)|10.20.30.40|:443... connected. 
HTTP request sent, awaiting response... 302 Found 
Location: /detaisl [following] 
--2016-11-15 05:18:02-- https://www.example.com/login 
Reusing existing connection to www.example.com:443. 
HTTP request sent, awaiting response... 302 Found 
Location: https://sso.example.com/idp/SSO.saml2?SAMLRequest=...something not related... [following] 
--2016-11-15 05:18:02-- https://sso.example.com/idp/SSO.saml2?SAMLRequest=...something not related... 
Resolving sso.example.com (sso.example.com)... 11.22.33.44 
Connecting to sso.example.com (sso.example.com)|11.22.33.44|:443... connected. 
HTTP request sent, awaiting response... 302 Found 
Location: https://sso.example.com/idp/95wM4/resumeSAML20/idp/SSO.ping [following] 
--2016-11-15 05:18:02-- https://sso.example.com/idp/95wM4/resumeSAML20/idp/SSO.ping 
Reusing existing connection to sso.example.com:443. 
HTTP request sent, awaiting response... 401 Unauthorized 

Username/Password Authentication Failed. 

當使用selenium2(硒-java的:3.0.1)和phantomjs-2.1.1-Linux的x86_64的用下面的代碼

WebDriver webdriver = new PhantomJSDriver(); 
webdriver.get("https://www.example.com/details"); 
System.out.println(webdriver.getCurrentUrl()); 
System.out.println(webdriver.getTitle()); 
System.out.println(webdriver.getPageSource()); 

的輸出是:

Nov 15, 2016 5:36:07 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> 
INFO: executable: ./phantomjs-2.1.1-linux-x86_64/bin/phantomjs 
Nov 15, 2016 5:36:07 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> 
INFO: port: 27571 
Nov 15, 2016 5:36:07 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> 
INFO: arguments: [--webdriver=27571, --webdriver-logfile=./phantomjsdriver.log] 
Nov 15, 2016 5:36:07 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> 
INFO: environment: {} 
[INFO - 2016-11-15T13:36:07.904Z] GhostDriver - Main - running on port 27571 
[INFO - 2016-11-15T13:36:08.113Z] Session [76cd8ec0-ab38-11e6-bceb-256426a0974e] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true} 
[INFO - 2016-11-15T13:36:08.113Z] Session [76cd8ec0-ab38-11e6-bceb-256426a0974e] - page.customHeaders: - {} 
[INFO - 2016-11-15T13:36:08.113Z] Session [76cd8ec0-ab38-11e6-bceb-256426a0974e] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}} 
[INFO - 2016-11-15T13:36:08.115Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 76cd8ec0-ab38-11e6-bceb-256426a0974e 
about:blank 

<html><head></head><body></body></html> 

因此,它只是打開about:空白頁面而已。有沒有辦法將用戶名和密碼輸入到彈出對話框中,並繼續訪問?

回答

0

使用此代碼來處理這種類型的錯誤,大部分時間我們得到SSL握手錯誤,我會建議您使用下面的代碼來處理這種類型的錯誤。

DesiredCapabilities caps = new DesiredCapabilities(); 
      caps.setJavascriptEnabled(true);     
      caps.setCapability("takesScreenshot", true); 
      caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,**Path**//phantomjs.exe");         

//處理SSL錯誤

caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[]{"--ignore-ssl-errors=true"}); 

//如果使用WebDriverWait禁用日誌

caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[]{"--webdriver-loglevel=NONE"}); 

    driver = new PhantomJSDriver(caps); 

,我會建議你從PhantomJS禁用日誌。

相關問題