[PYTHON] Since I am free, I make an "RPA tool" Extra edition

Introduction

Thank you everyone. This is enp. I like summer better than winter. This article will be a progress report for anyone trying to make an "RPA tool" from now on. Please note that it does not describe how to make RPA or RPA tools. This time, I would like to introduce RPA, which is an extension of # 3 because it is an extra edition. It's a very selfish RPA, so it feels like a gray zone, but thank you for your cooperation.

It's a hassle!

Yes. The title is just that. I think there are many people who think the same thing.

Why does Mabinogi have to be launched from a browser?

This applies not only to Mabinogi but also to all games launched only from a browser. __ It's a hassle to go to the homepage one by one! I want to do something! __ </ font> So I think. __ Can it be automated? __ You can now log in automatically with # 3. Therefore, you can log in to Mabinogi. All you have to do is click the button to launch the game! I started to think so.

Things needed for implementation

I'm starting automation right away, but here are some things I need. Therefore, I would like to list them here.

__ [List of things required for implementation] __ __ · Python__ __ ・ selenium__ __ · ChromeDriver__ __ ・ PyOTP__ __ · User profile that has launched the game once (important) __

From Python to PyOTP are the things you need to operate your browser automatically. The last "User profile that has launched the game once" is used to solve a certain problem. A user profile is a user's file __ that is synced with __Chrome.

The biggest wall "modal dialog"

Well, let's implement it immediately. However, there is one big problem. That is the existence of __modal dialogs __. A modal dialog is a dialog __ that can only be operated with the __ dialog. It feels like the dialog is about to collapse in Gestalt. Let's make it a little easier to understand. モーダルダイアログ.png The figure above is the modal dialog in question. While this dialog is displayed, clicking on Mabinogi fan art, announcements, etc. does nothing. A person who can only operate the __ dialog like this is called a modal dialog. __ But __ This dialog was not recognized by selenium. There is a function of __selenium that presses a dialog button, but it does not respond. I tried to use javaScript, but I couldn't get the button because I didn't know the class name in the first place. Now, I have a question for everyone who is playing Mabinogi here. __ Have you seen this dialog? __ I think those who played Mabinogi for the first time will have a new memory, but those who have been playing Mabinogi for many years are already beyond oblivion. Yes, this dialog usually doesn't appear after the second __. __ Well, only those who checked the checkbox. Then why does it disappear? __ Isn't it saved by Nexon? Those who thought, __ incorrect __ </ font>. However, it is not stored on the __web page. __ Web pages are created __ HTML, javaScript, etc. do not have the function to save information </ font> __. I think it's possible to save it in combination with a database, but it's not common. __ Then what? __ The answer is a small file called cookie. You may have heard of it. I will not talk about the mechanism in detail, but __ It will not be displayed if there is only a cookie saying "I have launched the game once". </ font> __ You don't have to deal with modal dialogs or anything you don't understand anymore. Therefore, __ "user profile that has launched the game once" becomes important. __ Because I have the cookies I need.

Get to know Chrome

I know I can use cookies to overcome modal dialogs. However, some people may think this way.

Do I have to bother to use a user profile?

The answer is yes. Because __Chrome manages cookies by user profile, it's __. However, this "managed by user profile" is only my impression. __ Therefore, please note that it is not correct. The reason why I felt "managed by user profile" is because the dialog problem was not solved by simply getting the cookie with __selenium __. Also, I thought so because the cookies I have are different for each __ user profile. In addition, I tried to use the default user profile, but I didn't use it because it caused some inconvenience. The inconvenience is that the program will be corrupted when Chrome is open. Apparently, it throws an error when the referenced directory is covered. __what the hell. __ Therefore, we have prepared a directory for individual user profiles.

Finally implemented

I've talked to Gudaguda so far, but the point is that I hope you can suppress the following two points. __ · Cookies required to resolve dialog issues __ __ · The dialog problem can be solved by using a user profile with the required cookies __ Yes. We will implement it based on these two points. The program is as follows.

nexon.py


#Import modules etc.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import chromedriver_binary
import pyotp
import time

#Driver settings
options = Options()
PROFILE_PATH = r"Individually prepared user profile directory"
options.add_argument('--incognito')  #Open browser in incognito mode
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument("--user-data-dir=" + PROFILE_PATH)   #Specifying the user profile directory
options.add_argument("--profile-directory=Profile 1")   #Specifying a user profile

#Driver designation
chrome = webdriver.Chrome(options=options)

chrome.get("https://login.nexon.co.jp/login/?gm=mabinogi")   #Go to login page
chrome.implicitly_wait(10)   #Wait until the required element is displayed (up to 10 seconds)

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

#Get a text box to enter login information
element_user = chrome.find_element_by_id("NexonID")
element_password = chrome.find_element_by_id("Password")
element_totp = chrome.find_element_by_id("OTP")

#Enter user name etc.
element_user.send_keys(user_name)
element_password.send_keys(password)

#Generate one-time password
totp = pyotp.TOTP("Alphanumeric characters used when setting a one-time password")
#Set the one-time password in advance
#Make a note of the required alphanumeric characters

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

#Click the game start button
btn = chrome.find_element_by_xpath("//div[@id='left']/div[@class='bt-login']/div[@class='btn-web-gamestart']/a")
btn.click()

#Wait (because the game did not start without waiting)
time.sleep(10)

#Close chrome
chrome.quit()

Cookies seem to be available when you run them in your user profile and you don't need to do anything. Also, please note that the game will not start if you use headless mode. Also, you will be asked for administrator permission to launch the game, click on it yourself. The reason is that it is not under the jurisdiction that can be operated with a web browser. But unfortunately __ this program has a fatal drawback. __ __ The dialog problem reoccurs when the cookie expires. __ Then you will also need to launch the game yourself using your user profile to create cookies.

Finally

Thank you for staying with us until the end. How was the extra edition? I've talked a lot with Gudaguda, but I hope this article helps something. However, please note that it is a fairly gray zone program. That's all for enp.

[Addition 8/15] I changed the program a little. The change is in options.add_argument ('--incognito'). This setting will open Chrome in secret mode. I read articles such as "The system is less likely to malfunction" as to why it opens in secret mode, but I am not. I simply didn't want to keep a history. Personally, I really hate the situation where "my information is leaked without my knowledge". So I don't usually want to keep a lot of history. I don't want to see my history without knowing it. You can see the history separately. But isn't it different? What can be seen without permission. Therefore, in order to eliminate the risk of being seen without permission, I try not to leave a history. I talked a little strange thing, but it's okay if you can understand only that "I changed the program a little"!