Let's create a program that automatically registers ID/PW from CSV to Bitwarden with Python + Selenium

Introduction

Password management is difficult, isn't it?

I've been using Trend Micro's Password Manager until now. Sometimes Chrome extensions are useless, and I was wondering if something was wrong. Bitwarden was free and it felt good, so I decided to switch.

Output the password file to CSV with Password Manager I thought it would be a moment to transfer after importing with Bitwarden, Since Password Manager was not an option in the import option, I tried to automate it with Selenium.

Environment

MacOS: Catalina 10.15.7 Python: 3.8.3 Selenium:: 3.141.0 Google Chrome: 87.0.4280.88 chromedriver: 87.0.4280.88

Bitwarden membership registration

First, let's register as a member of Bitwarden. It's nice that the UI looks like it is now.

https://vault.bitwarden.com/#/

Selenium environment construction

I set it by referring to this article.

Python + Selenium to go through all the automatic operations of Chrome https://qiita.com/memakura/items/20a02161fa7e18d8a693

Install Selenium with the following command.

pip install selenium

I tried to install chromedriver with pip install, In my case, the version did not match with Chrome and I got an error, so I have dropped the matching version of chromedriver from the following site.

http://chromedriver.chromium.org/downloads

After downloading, please place it wherever you like. I created and stored the following folders under the library.

/Library/ChromeDriver/chromedriver

This completes the Selenium setup.

Import required libraries

We will import the libraries used this time.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import datetime
import csv
import codecs

Access Bitwarden with chromedriver

People like me who downloaded the chrome driver from the site You need to specify the path as shown below.
driver = webdriver.Chrome(executable_path='/Library/ChromeDriver/chromedriver')

#Specify the URL of the website to access
driver.get('https://vault.bitwarden.com/#/')

#Wait 2 seconds after transitioning to the login screen
time.sleep(2)

#Close browser
driver.quit()

For those who pip installed the chrome driver

import chromedriver_binary

Please add.

You now have a program to access Bitwarden.

Login implementation

Since I was able to go to the login screen, I will try to enter the login ID and password and click the login button.
driver = webdriver.Chrome(executable_path='/Library/ChromeDriver/chromedriver')

#Specify the URL of the website to access
driver.get('https://vault.bitwarden.com/#/')

#Wait 2 seconds after transitioning to the login screen
time.sleep(2)

#Enter your login ID
login_id = driver.find_element_by_name("Email")
login_id.send_keys("My email address")

#Enter password
master_password = driver.find_element_by_name("MasterPassword")
master_password.send_keys("My password")

#Login button click
login_btn = driver.find_element_by_xpath("/html/body/app-root/app-frontend-layout/app-login/form/div/div/div/div/div[4]/button")
login_btn.click()

time.sleep(5)

#Close browser
driver.quit()

The login ID/password form is

find_element_by_name

You can find it at. Since the name was not specified for the login button, it is specified in XPATH.

Press the F12 button on your browser and use the developer tools to find the name or id of the location. It feels like specifying it with the corresponding "find_element_by ~".

You can now log in.

Read the URL/ID/PW of the site you want to register from the CSV file and register it

Since there were nearly 200 sites I wanted to register, I decided that manual work was impossible. Read from the CSV file and register.

By the way, this CSV file has the following format.

Passwords.csv


Site name,Site URL,username,password
driver = webdriver.Chrome(executable_path='/Library/ChromeDriver/chromedriver')

#Specify the URL of the website to access
driver.get('https://vault.bitwarden.com/#/')

#Wait 2 seconds after transitioning to the login screen
time.sleep(2)

#Enter your login ID
login_id = driver.find_element_by_name("Email")
login_id.send_keys("My email address")

#Enter password
master_password = driver.find_element_by_name("MasterPassword")
master_password.send_keys("My password")

#Login button click
login_btn = driver.find_element_by_xpath("/html/body/app-root/app-frontend-layout/app-login/form/div/div/div/div/div[4]/button")
login_btn.click()

time.sleep(5)

#Read CSV file
#Since Japanese is handled by the site name, the character code is Shit-Select JIS
with codecs.open('CSV file PATH',"rb", "Shift-JIS", "ignore") as f:
    for row in reader:
        #If there is a header on the first line of the CSV file, skip reading below
        next(reader)

        #Login ID/Get the elements of each form in the same way as PW and click/Enter

        #Item addition
        add_item = driver.find_element_by_xpath("/html/body/app-root/app-user-layout/app-vault/div/div/div[2]/div/div/button")
        add_item.click()

        #Enter name
        enter_name = driver.find_element_by_name("Name")
        enter_name.send_keys(row[0])
        print("I entered the name")

        #Enter user name
        enter_username = driver.find_elements_by_id("loginUsername")
        enter_username[0].send_keys(row[2])
        print("I entered my user name")

        #Enter password
        enter_password = driver.find_elements_by_id("loginPassword")
        enter_password[0].send_keys(row[3])
        print("I entered the password")

        #Enter URL
        enter_url = driver.find_elements_by_xpath("/html/body/app-root/app-user-layout/app-vault/app-modal/app-vault-add-edit/div/div/form/div[2]/div[5]/div[1]/div/input")
        enter_url[0].send_keys(row[1])
        print("I entered the URL")

        #Click the save button
        click_save = driver.find_elements_by_xpath("/html/body/app-root/app-user-layout/app-vault/app-modal/app-vault-add-edit/div/div/form/div[3]/button[1]/span")
        click_save[0].click()

        #Wait 2 seconds until the next input
        time.sleep(2)

#Close browser
driver.quit()

Items that are not picked up by name or id are specified in XPATH.

enter_username[0].send_keys(row[2])

Things with [0] are necessary because the variable type is list. If you run it without this

AttributeError: 'list' object has no attribute 'send_keys',

I got the error.

With reference to this, I tried to imitate it for the time being. I'm not sure why, but it worked, so I left it for the time being. .. ..

https://programmersought.com/article/33681438536/

that's all.

Reference

> Python + Selenium to go through all the automatic operations of Chrome >https://qiita.com/memakura/items/20a02161fa7e18d8a693

AttributeError: 'list' object has no attribute 'send_keys', python+selenium Enables automatic login of QQ space pages. https://programmersought.com/article/33681438536/

Recommended Posts

Let's create a program that automatically registers ID/PW from CSV to Bitwarden with Python + Selenium
Let's create a script that registers with Ideone.com in Python.
Create a tool to automatically furigana with html using Mecab from Python3
From buying a computer to running a program with python
Python script to create a JSON file from a CSV file
Create a Mastodon bot with a function to automatically reply with Python
Create folders from '01' to '12' with python
I tried to make a generator that generates a C # container class from CSV with Python
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
Create a program that can generate your favorite images with Selenium
[Python] A program that creates stairs with #
Let's create a free group with Python
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
[Outlook] I tried to automatically create a daily report email with Python
Let's create a customer database that automatically issues a QR code in Python
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
Create a page that loads infinitely with python
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
I tried to easily create a fully automatic attendance system with Selenium + Python
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
[Python] How to create a 2D histogram with Matplotlib
[Python] Create a Tkinter program distribution file with cx_Freeze
How to create a kubernetes pod from python code
[Python] I tried to automatically create a daily report of YWT with Outlook mail
Try to create a waveform (audio spectrum) that moves according to the sound with python
[It's not too late to learn Python from 2020] Part 2 Let's create a Python development environment
A memo that reads data from dashDB with Python & Spark
I tried to automatically create a report with Markov chain
How to run a Python program from within a shell script
A learning roadmap that allows you to develop and publish services from scratch with Python
Probably the easiest way to create a pdf with Python3
I made a program to collect images in tweets that I liked on twitter with Python
I want to automatically attend online classes with Python + Selenium!
Write to csv with Python
A story that made it possible to automatically create anison playlists from your music files
Create a message corresponding to localization with python translation string
Create a directory with python
A Python program that aggregates time usage from icalendar data
A python script that converts Oracle Database data to csv
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
How to batch start a python program created with Jupyter notebook
A memorandum when I tried to get it automatically with selenium
[Python] A memo that I tried to get started with asyncio
Try to create a python environment with Visual Studio Code & WSL
How to create a heatmap with an arbitrary domain in Python
Simple code to call a python program from Javascript on EC2
I tried to make a periodical process with Selenium and Python
[Ev3dev] Create a program that captures the LCD (screen) using python
Let's stop copying. Introducing flati, a module to flatten with Python
[LINE Messaging API] Create a BOT that connects with someone with Python
[Python algorithm] A program that outputs Sudoku answers from a depth-first search
Let's feel like a material researcher with python [Introduction to pymatgen]
Let's create a Python directory structure that you won't regret later
I tried to create Bulls and Cows with a shell program
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
Create a poster with matplotlib to visualize multiplication tables that remember multiplication
Pass a list by reference from Python to C ++ with pybind11