ABSP, Ch 11, Practice Project #1

My solution to the Practice Project #1 in Chapter 11 of the excellent book “Automate the Boring Stuff with Python” (ABSP)

#! usr/bin/env python3
# by lorenzo - 10/10/2017
# command line emailer

from selenium import webdriver

print("Enter e-mail address of recipient:")
recipient = input()
print("Enter subject of the e-mail:")
subject = input()
print("Enter text of the e-mail:")
emailText = input()

browser = webdriver.Firefox()
browser.get("myEmailProvider")
emailElem = browser.find_element_by_id("usernameDisplay")
emailElem.send_keys("myUsername")
passwordElem = browser.find_element_by_id("password")
passwordElem.send_keys("myPassword")
passwordElem.submit()

# I've successfully tested the code up to this point
# because I haven't been able to make selenium click on the "Write" button.
# I will try to amend the code asap

writeElem = browser.find_element_by_class_name("write") # this doesn't work 😦
writeElem.click()
recipientElem = browser.find_element_by_id("composeTO")
recipientElem.send_keys(recipient)
subjectElem = browser.find_element_by_id("composeSUBJECT")
subjectElem.send_keys(subject)
messageElem = browser.find_element_by_id("messageComposeEditor")
messageElem.send_keys(emailText)
sendElem = browser.find_element_by_class_name("sendNormalmail")
sendElem.click()
exitElems = browser.find_element_by_class_name("exit")
exitElems.click()
browser.quit()

NOTE:

  • to make send_keys work properly* set the default language of the computer to “English”
  • to make browser = webdriver.Firefox() work properly install geckodriver with the following command: brew install geckodriver

*otherwise it might eat some of the characters that you want selenium to write

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s