【基础】使用cookies,实现免登陆(七)
生活随笔
收集整理的這篇文章主要介紹了
【基础】使用cookies,实现免登陆(七)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現過程:
1.測試網站:www.dx.com
2.登陸后的cookies:"DXSSO","Token=20A0FA7D-XXXX-XXXX-XXXX"。至于為什么是這個cookies,可以根據登錄前后的cookie對比。
3.上代碼:
public class TestCookies {public static void main(String[] args) throws InterruptedException {System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");ChromeDriver driver = new ChromeDriver();driver.manage().window().maximize();driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);try{driver.get("https://www.dx.com/");}catch(Exception e){System.out.println("page load timeout");}Cookie c = new Cookie("DXSSO","Token=XXXXXXXXXXXXXXXXXXXXXXXXXX");driver.manage().addCookie(c);Thread.sleep(3000);try{driver.get("https://www.dx.com/");//driver.get("https://my.dx.com/");}catch(Exception e){System.out.println("page load timeout");}driver.quit();}}
上述代碼可以實現,免登錄功能。
?
在測試過程中,有兩個問題:
1.如果不打開一個網頁,而直接加載cookies的時候,會報錯:
Exception in thread "main" org.openqa.selenium.WebDriverException: <unknown>: Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs.(Session info: chrome=56.0.2924.87)(Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 50 milliseconds Build info: version: '3.4.0', revision: 'unknown', time: 'unknown' System info: host: 'LANMENG-PC', ip: '192.168.27.48', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_131' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf), userDataDir=C:\Users\ADMINI~1\AppData\Local\Temp\scoped_dir8872_4962}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=56.0.2924.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 3c6ab801956ea5a955a76b37edc4eea3at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)at java.lang.reflect.Constructor.newInstance(Unknown Source)at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.addCookie(RemoteWebDriver.java:764)at com.dx.testcase.TestCookies.main(TestCookies.java:21)2.如果在www.dx.com頁面加載的cookies,在my.dx.com頁面,則還是未登錄狀態。感覺是更換了域名,則cookies不能通用。
轉載于:https://www.cnblogs.com/Jourly/p/8350281.html
總結
以上是生活随笔為你收集整理的【基础】使用cookies,实现免登陆(七)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 137 Single Number II
- 下一篇: itertools库