"Let Python do the boring things" exercise ~ Command line mailer ~

I can't find a vacancy unless I keep an eye on the skill training reservation of the driving school 24 hours a day, so I'm studying python to automate the system.

I made a command line electronic mailer that came out in Exercise 11.10.1 of the title book, so I tried to organize it.

I used Excite mail this time, but the reason is that Gmail and yahoo had some kind of multi-step authentication, so it was Excite mail that I could easily log in with just my id and password.

environment

policy

--Receive email address and text from command line --Log in to Excite mail with selenium --Create a new email and send the text to the email address you received as an argument

Program policy

--Use the sys module to receive text at the email address sys.argv [2] at sys.argv [1]. --Open the login page of Excite mail with the webdriver of the selenium module, send the user name and password with sendKeys, and log in with submit (). --Open the email sending screen from the newly created link, enter the email address and text received as an argument, and submit ()

Implementation

CommandlineMailer.py


#! python 3.6.0
# commnadlineMailer.py -Send text to the email address you received on the command line
# Usage:
# commnadlinMailder.py <[email protected]> <sometext> -Email address as the first argument,Receive text as the second argument

import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

if len(sys.argv) > 2:
    email_adress = str(sys.argv[1])
    main_text = str(sys.argv[2])

    #Open Excite email and log in
    browser = webdriver.Safari()
    browser.get('https://ssl2.excite.co.jp/idc/login/?si=mail&ru=http%3A%2F%2Femail.excite.co.jp')
    #I used the name tag because there was no id tag
    email_elem = browser.find_element_by_name('loginid') 
    email_elem.send_keys('[email protected]')

    password_elem = browser.find_element_by_id('id-password')
    password_elem.send_keys('yourPassword')
    password_elem.submit()

    #After waiting for the page to be displayed, open the new creation screen
    link_elem = WebDriverWait(browser, 10).until(
        EC.element_to_be_clickable((By.LINK_TEXT, 'Create New'))
    )
    link_elem.click()

    #After waiting for the page to be displayed, enter the email address and text and send
    to_elem = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.ID, 'to'))
    )
    to_elem.send_keys(email_adress)

    main_text_elem = browser.find_element_by_id('body')
    main_text_elem.send_keys(main_text)

    send_elem = browser.find_element_by_id('_sendmail')
    send_elem.submit()
else:
    print('There are few arguments')

The point that got caught

At first, I was implementing it without waiting for the display, and the error came out and I felt like "?", But I noticed that an error occurred when I accessed it when it was not displayed.

The point was to use WebDriverWait to stop processing "until a specific element appears ≒ until the update is successful". If it cannot be updated within the 10 seconds specified by WebDriverWait (browser, 10), a timeout error will be returned.

The point that I cut corners

Since it is used only in the exercise, I omitted checking whether it is an email address with a regular expression and processing when the argument is reversed.

Recommended Posts

"Let Python do the boring things" exercise ~ Command line mailer ~
Receive the form in Python and do various things
[2020 version] Let Python do all the tax and take-home calculations
python Note: Determine if command line arguments are in the list
MoneyForward Cloud Automates time stamping [Let's let Python do the trouble]
Python standard module that can be used on the command line
Command line argument processing (Python docopt)
Try the Python LINE Pay SDK
In the python command python points to python3.8
Command for the current directory Python
Keep getting RSS on the command line
Let Heroku do background processing with Python
[Python] Summarize the rudimentary things about multithreading
Read the file line by line in Python
Decompose command arguments in one line in Python
You search commandlinefu on the command line
[Python] Read the specified line in the file
Correspondence memo when the direction key cannot be used on the python command line