[PYTHON] Since I am free, I will make an "RPA tool" # 3 selenium edition Login automation

Introduction

Everyone, it's been a month or so. This is enp. I'm wearing glasses. This article is a progress report for anyone trying to make an "RPA tool". Please note that it does not describe how to make RPA or RPA tools. (Maybe I'll put a little sauce on it, it depends on my mood) This time is Part.1 to get along with selenium. The only thing I did was "login operation", but it was quite profound. It will be a long article again, but I would appreciate it if you could get along with me.

What can selenium do?

First, let's get acquainted with selenium. selenium is a tool that directly operates the browser with __ program. __ Instead of using the mouse pointer to operate the browser It's like selenium issuing commands to the browser to operate. So you will be able to do other work while running your browser on selenium. (I haven't tried the one with long operating time, so I haven't verified how it actually works.)

Let's log in with selenium Nico Nico Douga

Yes. From here, the main subject is to automate login with __selenium. __ The reason for automating login is that it seems easy and is perfect for practicing selenium. __ You can log in with basic actions such as typing in a text box or clicking a button. But __ wasn't that sweet. I'll talk about that later. __ </ font>

Now, let's log in immediately. To get started, log in to Nico Douga. The following is the login screen of Nico Douga. ニコ動のログイン画面 Well, it looks like this. There is also login with google below, but I will not use it this time, so I do not post it. (The copyright of the logo was also scary)

As a flow, __1. Jump to the login page. __ __2. Enter your email address in the text box above __ __3. Enter your password in the text box below __ __4. Click the login button __ That's right.

My implementation method is __ "Put the procedure I usually do into programming as it is" __. So __ I will implement the above flow as it is. __

The code is below.

slogin_niconico.py


#Driver import
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import psutil

chrome = webdriver.Chrome(r".\driver\chromedriver.exe")
 
#Go to login page
chrome.get("https://account.nicovideo.jp/login?site=nicoaccount&next_url=%2Fmy%2Faccount%3F&time=1593735684")

#Variables required for login
user_name = "user_name"
password = "password"

#Get textbox and login button
element_user = chrome.find_element_by_id("input__mailtel")
element_password = chrome.find_element_by_id("input__password")
element_logbtn = chrome.find_element_by_id("login__submit")

#Enter your address and password in the text box
element_user.send_keys(user_name)
element_password.send_keys(password)

#Press the login button
element_logbtn.click()

#Terminate the chromedriver process
p = psutil.Process(chrome.service.process.pid)
p.terminate()

Something like this. You really just jump to the login page, type in the textbox and press the login button. The last step is to deal with the problem that the driver keeps running even after the program ends due to the specifications. What we're doing is using the process ID to dismiss the driver's process. If you have any other concerns, please check for yourself! It is just a progress report.

Let's log in with selenium Amazon edition

Now that you have logged in to Nico Douga, let's log in to Amazon. __Hmm? What is different from Nico Douga? __ You might think that. __ The flow is certainly almost the same as when Nico Douga __. However, Amazon adds a bit of work. amazonのログイン画面.png __ Yes! Two-step certification! __ </ font> A one-time password is required. (Although Nico Douga also has two-step authentication, this time I used it as a non-version) It took me a month to figure out how to get through this one-time password. Of course, selenium doesn't have a nice feature to get through two-step authentication. This was a problem ... So, the story from here will be sent in a two-stage certification breakthrough struggle record, a luxurious double-barrel.

Strategy.1 Use of an app that generates a one-time password

This strategy is to use the subprocess module to get a one-time password from an external app such as Google's Google Authenticator __. However, it is difficult to handle apps that run on .exe such as Google Authenticator, so I tried to implement __ using an app that runs on the __ command prompt. I found the following two.

  • oath-toolkit
  • authenticator

I will implement it with either of these two, but the one I chose is authenticator. The reason is __ because it was easy to use as a license. The best MIT license. However, authenticator also has one drawback. It doesn't end forever unless you exit with __Ctrl-C. __ You can't get the output of __authenticator until it's done forever. __ That's where the __subprocess module's terminate method comes in handy. __ You can use the terminate method to kill __authenticator. __ This is the only way to do it, as you will be able to get the output when you are done. And the result obtained by the combined technique of authenticator and subprocess module is as follows.

Enter passphrase:

Why! !! Why can't I get a one-time password!

The reason seems to be __ I can't get the output of the interrupted part __. It seems that the reason why only the Enter passphrase part can be obtained is that the input wait comes later. It may be different. Well, the important thing here is that the __ subprocess module can't get the one-time password generated by the authenticator __ </ font>. To put it plainly, it's __operation failure __. I learned how to use the subprocess module. About a month.

Strategy.2 You should generate it yourself

This strategy is __RPA's own one-time password generation __. That's right. __ It's a hassle, so let's incorporate a guy like authenticator into RPA. __ If RPA itself can generate a one-time password, everything will be solved. No external apps are needed. That's where the __pyotp library comes in. __ This library is used when implementing two-step authentication in Python, but this time it is used to break through two-step authentication. How to use it is as follows.

#Import pyotp library
import pyotp

#One-time password generation
totp = pyotp.TOTP("Alphanumeric characters to enter when setting up Google Authenticator")
totp.now()

Amazingly easy!

I'm surprised that you can break through the one-time password with this. An extra month of effort remains as sadness. It has been confirmed that it actually works. The __operation was successful! __ I didn't have to use the back hand of this application.

This is the end of the struggle to break through the two-stage certification. Well, it was a deep content that I was doing. The subprocess I didn't use in particular. Now that you've come this far, all you have to do is write a program. The program I wrote is as follows.

slogin_amazon.py


#Driver import
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import psutil
import pyotp

chrome = webdriver.Chrome(r".\driver\chromedriver.exe")

#Go to login page
chrome.get("https://www.amazon.co.jp/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.co.jp%2F%3Fref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=jpflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&")

#Variables required for login
user_name = "user_name"
password = "password"

#Get the text box on the first page
element_user = chrome.find_element_by_name("email")

#Enter your email address (press Enter)
element_user.send_keys(user_name)
element_user.send_keys(Keys.ENTER)

#Get the text box on the second page
element_password = chrome.find_element_by_name("password")

#Enter password (press Enter)
element_password.send_keys(password)
element_password.send_keys(Keys.ENTER)

#One-time password generation
element_totp = chrome.find_element_by_name("otpCode")
totp = pyotp.TOTP("Alphanumeric characters to enter when setting up Google Authenticator")

#Enter the one-time password (press Enter)
element_totp.send_keys(totp.now())
element_totp.send_keys(Keys.ENTER)

#Terminate the chromedriver process
p = psutil.Process(chrome.service.process.pid) 
p.terminate()

I feel like this. This time, instead of clicking the button, I implemented it by pressing Enter. That's all I did this time.

What you need to do to implement RPA tools

After going through the login automation, I got a little understanding of the selenium specifications. In this item, I would like to enumerate the contents related to the implementation of RPA tools in this time. The bullet points follow.

--_ You need to have your browser selected to specify the driver. __ --_ You need to enter the class name and id name to get the text box. __ --_ You need to be asked to select what you want to use to get the text box etc. __ --_ __ You need to have each text box specify what you want to enter. __ --_ If you need two-step authentication, you need to have each account go through steps like setting up Google Authenticator once. __ ――_ It is necessary for the developer to send something such as the class name so that it can be used by people who cannot program. __ --_ When you're done using the driver, you need to kill the process. __

That's about it as far as I can think of.

Finally

Thank you for staying with us until the end. I posted the source I wrote for the first time, how was it? The only thing I did this time was "login automation", but it was challenging. I think the next article will be "Let's get along with selenium Part.2", so thank you. That's all for enp.

[Addition] We received the indication of the source correction and adopted it. Thank you, shiracamus. If there is a directory that starts with n etc. unless it is a raw string, There is a risk that it will not work because it is not recognized as a normal . It may be the basis, but it was a very valuable opinion for me.