Selenium webdriver Processing Browser cookies

Source: Internet
Author: User

Sometimes we need to verify that there is a cookie in the browser because testing based on a real cookie is not possible through white boxes and
Integration testing is done. Webdriver provides a way to manipulate cookies to read, add, and delete cookie information.
Webdriver How to manipulate cookies are:
? GetCookies () Get all cookie information
? Addcookie (cookie_dict) to add a cookie, the name and value must be
? Deleteallcookies () Delete all cookie information
? Deletecookienamed (String name). Delete Name this cookie
? Getcookienamed (String name). Returns the cookie value for a specific name


The following is a getcookies () to obtain cookie information for the current browser.

 Packagecom.mypro.jase;ImportJava.util.Set;ImportOrg.openqa.selenium.Cookie;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.chrome.ChromeDriver; Public classCookie { Public Static voidMain (string[] args) {Webdriver driver=NewChromedriver (); Driver.get ("Http://www.youdao.com"); //Get CookiesSet<cookie> COO =driver.manage (). GetCookies (); //Print CookiesSystem.out.println (COO);     Driver.quit (); }}

Execution Result:

>>> ================================ RESTART ================================
>>>
[Jsessionid=abc6pheyjzl7m_fdlpy3u;
path=/;
Domain=www.youdao.com,
[Email protected];
expires= Thursday, 16 months 2045 10:08:10 CST;
path=/;
Domain=.youdao.com]

By printing the results can be seen, the cookie is stored in the form of a dictionary, know the form of cookie storage, then we will
Cookie information can be written to the browser in this form.

ImportJava.util.Set;ImportOrg.openqa.selenium.Cookie;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.chrome.ChromeDriver; Public classCookie { Public Static voidMain (string[] args) {Webdriver driver=NewChromedriver (); Driver.get ("Http://www.youdao.com"); Cookie C1=NewCookie ("name", "KEY-AAAAAAA"); Cookie C2=NewCookie ("Value", "VALUE-BBBBBB"));            Driver.manage (). Addcookie (C1);            Driver.manage (). Addcookie (C2); //Delete all Cookiesdriver.manage (). Deleteallcookies (); //Get CookiesSet<cookie> COO =driver.manage (). GetCookies (); //Print CookiesSystem.out.println (COO);     Driver.quit (); }}

Execution Result:
>>> ================================ RESTART ================================
>>>
[Name=key-aaaaaaa;
path=/;
Domain=www.youdao.com,
VALUE=VALUE-BBBBBB;
path=/;
Domain=www.youdao.com]

The last cookie information that can be seen from the printed result is added by the Addcookie () method during script execution. Through the Times
Information to find the value of a specific cookie with key "name" and "value".
Then under what circumstances will the operation of the cookie be used? For example, developers develop a feature that, when the user logs on, will
The user name is written to the browser cookie, the specified key is "username", then we can find the Useranme by GetCookies (),
Print Vlaue, if the username is not found or the corresponding value is null, then the cookie to save the browser is problematic.
The use of Deleteallcookies () is also simple, the former is deleted by the name value to a specific cookie, which directly deletes all cookies () information in the browser.

Selenium webdriver Processing Browser cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: [email protected] and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.