Log in by passing the login session cookie obtained by Python requests to the Selenium browser.

What are you saying

Suppose you have a web app whose client is supposed to be something other than a web browser, such as a mobile app.

It doesn't have a login page like you would see in a typical web service. This is because the client app contains a login form.

However, when doing automatic testing, I want to access it from the Selenium browser and see how it works. At that time.

code

python


import requests
from selenium import webdriver

cookie_name = 'session_id'

#Log in with requests and make a session cookie
s = requests.session()
login_data = {
    'email': '[email protected]',
    'password': 'xxxxxxxxxx',
}
res = s.post('https://example.com/login/', data=login_data)
res.raise_for_status()

#Session cookie value
cookie_value = s.cookies.get(cookie_name)

#Firefox launch
driver = webdriver.Firefox()
#Open an appropriate page for the target domain in advance.
#If you don't do this, add_I get an error with a cookie
driver.get('https://example.com/')
#Pass the session cookie obtained by requests to Firefox
driver.add_cookie({
        'name': cookie_name,
        'value': cookie_value,
        'domain': 'example.com'})
#Display the page after login
driver.get('https://example.com/mypage/')

Now you can see the page after logging in in Firefox.

The add_cookie method works in webdriver.Chrome as well, so you can log in in the same way. Maybe other web drivers will work as well.

Example that was useless

When I created HTML of a suitable form locally and set its action = as the login URL, it was requested by GET for some reason even if method = "POST". It seems that recent browsers can no longer send by POST in cross domain, probably because of security.

If you do your best, a plan that may be possible only with selenium

You may be able to log in by opening the appropriate page of the domain you are logging in to in Selenium, creating a login form in JS, and sending a POST from there. But I haven't done it because it seems to be difficult.

Recommended Posts

Log in by passing the login session cookie obtained by Python requests to the Selenium browser.
Log in to Slack using requests in Python
Log in to Yahoo Business with Selenium Python
Login to website in Python
How to determine the existence of a selenium element in Python
Python in the browser: Brython's recommendation
In the python command python points to python3.8
[pepper] Pass the JSON data obtained by python request to the tablet.
Sort the file names obtained by Python glob in numerical order
A story that didn't work when I tried to log in with the Python requests module
[Python] How to set the (client) window size inside the browser with Selenium
An example of the answer to the reference question of the study session. In python.
How to sort by specifying a column in the Python Numpy array.
Display Python 3 in the browser with MAMP
Read the file line by line in Python
Read the file line by line in Python
[Python] Automatically operate the browser with Selenium
How to read all the classes contained in * .py in the directory specified by Python
How to use the C library in Python
Log in to the remote server with SSH
Let's parse the git commit log in Python!
To dynamically replace the next method in python
Draw graphs in Julia ... Leave the graphs to Python
How to specify TLS version in python requests
The trick to write flatten concisely in python
How to erase the characters output by Python
How to get the files in the [Python] folder
Use pygogo to get the log in json.
Execute Power Query by passing arguments to Python
I want to display the progress in Python!
Open page in secret mode by specifying browser language with Selenium Python Bindings (Chrome)
Let's make a leap in the manufacturing industry by utilizing the Web in addition to Python
How to retrieve the nth largest value in Python
I tried to graph the packages installed in Python
When "Message: session not created" occurs in Python + Selenium
How to get the variable name itself in python
Upload the image downloaded by requests directly to S3
Note the frequently used options in Python + Selenium + Chrome
Read the xml file by referring to the Python tutorial
Convert the image in .zip to PDF with Python
I was addicted to scraping with Selenium (+ Python) in 2020
Get the value selected in Selenium Python VBA pull-down
Write data to KINTONE using the Python requests module
Shift the alphabet string by N characters in Python
How to download files from Selenium in Python in Chrome
I want to write in Python! (3) Utilize the mock
Store the stock price scraped by Python in the DB
How to use the model learned in Lobe in Python
Try to decipher the login data stored in Firefox
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
I made a class to get the analysis result by MeCab in ndarray with python
How to check if the contents of the dictionary are the same in Python by hash value
[Selenium] I want to display the browser by hitting the driver on the host OS from WSL