get_cookies driver method - Selenium Python

Introduction:

In this tutorial, we are learning about the get_cookies driver method in Selenium Python. The Selenium module is used for automatic testing using Python. Selenium Python bindings provide a simple API. Using Selenium WebDriver, this API is used to write functional or acceptance tests. A web page can be opened using Selenium Python, checkout - Using Get Method, we can navigate the links - Selenium Python.

It is being able to go to the right place. We want to interact with the page. More specifically, the HTML content inside the page. There are many ideas for finding content using Selenium, such as Locating Strategies. Selenium WebDriver provides various methods to control sessions (e.g., browser). Some examples of Selenium WebDriver are adding cookies, pressing the back button, navigating between tabs, etc.

Selenium web driver can handle cookies. In Selenium, we can add cookies, receive cookies with specific names, and remove cookies with the help of various methods. This tutorial can revolve around the get_cookies driver in Selenium Python. The get_cookie method is used to get the cookie with the specified name. This method returns cookies if found or selects None if not found. The get_cookies method is used to display all the cookies that belong to the given URL.

Syntax:

The syntax of the get_cookies driver method in Selenium Python is given below -

Example:

Now we give an example of using the get_cookies method as a driver method, which we have given below -

Discuss with program code about the get_cookie driver method in Selenium Python.

Here, we learn the program code of the get_cookie method using a web driver in Selenium Python. Now, we visit the web page https://www.javatpoint.com/ and operate it on the driver object. By this method, we get the cookies with the specified name and return cookies if found or select None if not found. So, now we give the program code using Python, which is given below -

Program Code 1:

Output:

Now, we compile the above code in Python, and after successful compilation, we run it. We get cookies from the website using the get_cookie() method. The output is given below -

get_cookies driver method - Selenium Python

Program Code 2:

Now, we give another example of the get_cookie method using a web driver in Selenium Python. Here, we use the get_cookies method to display all the cookies that belong to the given URL. The get_cookie() and get_cookies methods are used in this given program. So, now we give the program code using Python, which is given below -

Output:

Now, we compile the above code in Python, and after successful compilation, we run it. We get website cookies using the get_cookie() method, and all cookies are obtained using the get_cookies() method. The output is given below -

The Cookie is:
{'domain': 'www.javatpoint.com', 'httpOnly': False, 'name': 'J1', 'path': '/', 'secure': True, 'value': 'val'}
The Cookies are:
[{'domain': 'www.javatpoint.com', 'httpOnly': False, 'name': 'J1', 'path': '/', 'secure': True, 'value': 'val'}]

Process finished with exit code 0

Conclusion:

So, in this tutorial, we are learning about the get_cookie driver method in Selenium Python. Selenium module is automatic testing using Python. This method is used to get the cookie of the web page. Here, we learn the syntax of the get_cookie driver method. We also learn the program code of the get_cookie driver method in Selenium Python and share the outputs of the given code.






Latest Courses