|
selenium+java,读取文件添加到cookie失败,org.openqa.selenium.InvalidCookieDomainExcepti
- /输入账号、密码登录网站,保存cookie到本地文件
- public static void saveCookie1(WebDriver driver) {
- // 打开百度网址
- driver.get("http://www.baidu.com");
- WebElement loginEntry = driver.findElement(By
- .xpath("//*[@id=\"u1\"]/a[7]"));
- loginEntry.click();
- //查找登录窗口
- Set<String> windowIds = driver.getWindowHandles();
- for (String windowId : windowIds) {
- if (driver.switchTo().window(windowId).getPageSource()
- .contains("下次自动登录")) {
- driver.switchTo().window(windowId);
- break;
- }
- }
- //等待WebElement元素加载
- WebElement userName = new WebDriverWait(driver, 10)
- .until(ExpectedConditions.presenceOfElementLocated(By
- .xpath("//*[@id=\"TANGRAM__PSP_8__userName\"]")));
- // 输入登录信息
- // WebElement userName = driver.
- // findElement(By.xpath("//*[@id=\"TANGRAM__PSP_8__userName\"]"));
- userName.sendKeys("1367115143@qq.com");
- WebElement passWord = driver.findElement(By
- .xpath("//*[@id=\"TANGRAM__PSP_8__password\"]"));
- passWord.sendKeys("8766502292273");
- WebElement member = driver.findElement(By.name("memberPass"));
- // 记住密码
- if (!member.isSelected()) {
- member.click();
- }
- // 点击登录按钮
- WebElement loginButton = driver.findElement(By
- .xpath("//*[@id=\"TANGRAM__PSP_8__submit\"]"));
- loginButton.click();
- // 保存cookie到本地
- File cookieFile = new File("baidu.cookie.txt");
- try {
- cookieFile.delete();
- cookieFile.createNewFile();
- FileWriter fileWriter = new FileWriter(cookieFile);
- BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
- System.out.println("cookie大小:"+driver.manage().getCookies().size());
- for (Cookie cookie : driver.manage().getCookies()) {
- bufferedWriter.write(cookie.getName() + ";" + cookie.getValue()
- + ";" + cookie.getPath()+";"+cookie.getDomain()
- + ";" + cookie.getExpiry() +";"+cookie.isSecure());
- bufferedWriter.newLine();
- System.out.println(cookie.getName()+"保存成功");
- }
- bufferedWriter.flush();
- bufferedWriter.close();
- fileWriter.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //从本地文件读取内容,添加到cookie中,实现打开网站时显示已登录
- public static void useCookie1(WebDriver driver){
- //读取cookie
- try{
- File cookieFile = new File("baidu.cookie.txt");
- FileReader fr = new FileReader(cookieFile);
- BufferedReader br = new BufferedReader(fr);
- String line;
- int i=0;
- //按行读取
- while( (line = br.readLine()) != null ){
- i++;
- System.out.print("第"+i+"行:");
- StringTokenizer stringTokenizer = new StringTokenizer(line,";");
- //读取每行逗号间的内容
- while(stringTokenizer.hasMoreTokens()){
- String name = stringTokenizer.nextToken();
- System.out.print(name);
- String value = stringTokenizer.nextToken();
- System.out.print(value);
- String domain = stringTokenizer.nextToken();
- System.out.print(domain);
- String path = stringTokenizer.nextToken();
- System.out.print(path);
- //处理日期,将读取的字符串转化为Date格式
- Date expiry = null;
- String dt = stringTokenizer.nextToken();
- if(! dt.equals("null") ){
- expiry = new Date(dt);
- }
- System.out.print(expiry);
- boolean isSecure = new Boolean(stringTokenizer.nextToken()).booleanValue();
- System.out.println(isSecure);
- //添加cookie到driver
- Cookie cookie = new Cookie(name,value,domain,path,expiry,isSecure);
- driver.manage().addCookie(cookie);
- }
- }
- }catch(Exception e){
- System.out.println();
- e.printStackTrace();
- }
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- driver.get("http://www.baidu.com");
- }
- 执行到函数useCookie1(WebDriver driver)->driver.manage().addCookie(cookie);添加失败
- 报错:
- org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
- Command duration or timeout: 87 milliseconds
- Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
- System info: host: 'PC', ip: '10.9.3.108', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
- Session ID: cbfec852-11db-4c71-97e8-a64a9ca8c4fc
- Driver info: org.openqa.selenium.firefox.FirefoxDriver
- Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=42.0}]
- 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:204)
- at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
- at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
- at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.addCookie(RemoteWebDriver.java:687)
- at cxl.selenium.fouth.UseCookie.useCookie2(UseCookie.java:69)
- at cxl.selenium.fouth.UseCookie.main(UseCookie.java:27)
- Caused by: org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
- Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
- System info: host: 'PC', ip: '10.9.3.108', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
- Driver info: driver.version: unknown
- at <anonymous class>.FirefoxDriver.prototype.addCookie(file:///C:/Users/admin/AppData/Local/Temp/anonymous5087220180491392428webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10409)
- at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/admin/AppData/Local/Temp/anonymous5087220180491392428webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12282)
- at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/admin/AppData/Local/Temp/anonymous5087220180491392428webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12287)
- at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/admin/AppData/Local/Temp/anonymous5087220180491392428webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12229)
- 十二月 01, 2015 4:12:20 下午 org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
- 信息: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@1567904d
复制代码
|
|