Handle Login with Python Bindings for Selenium
Before everything else, you need to install the Selenium package, of course.
1 | pip install selenium |
Or, if you hate to deal with anti-bot measures, you can just use this.
1 | pip install undetected-chromedriver |
Then, add the user data directory to the ChromeOptions object. It is the path to your Chrome profile. For macOS, it is located at ‘~/Library/Application Support/Google/Chrome’.
1 | import undetected_chromedriver as uc |
The --user-data-dir
argument is kind of cheating because it allows you to bypass the login process without actually logging in.
Cookie is your friend.
But sometimes, you need to handle the login process, for instance, you have to switch between multiple accounts.
First of all, take care of your credentials. Use an .env file.
1 | import os |
Then, you can use the send_keys
method to fill in the username and password fields. I add one while loop to wait for the element in case the script runs too fast.
1 | while True: |
After logging in, the chrome usally pops up a dialog asking if you want to save the password. It is annoying.
You can try to disable it by adding the --disable-save-password-bubble
or --disable-popup-blocking
argument to the ChromeOptions object. I don’t think it works. But you can try.
In the end, I just used a hack, that is to open a new tab and immediately close it, the popup will appear.
1 | # open a new tab |
That’s it.
Oh, one more thing.
Add user-agent to the ChromeOptions object is also a good idea. And please do not forget to specify version_main
for the driver to match your current chrome version.