实现过程:
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:: 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 millisecondsBuild 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.ChromeDriverCapabilities [{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: 3c6ab801956ea5a955a76b37edc4eea3 at 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不能通用。