How to auto-submit Microsoft Forms using python (Mac version)

Introduction

Recently, due to the influence of Are, I had to submit Microsoft Forms early every morning in my situation. Thinking of it as "cumbersome", I decided to implement Forms auto-answer while learning python web scraping.

1. Creation environment ・ MacOS X Catalina 10.15.7 ・ Chrome 86.0.4240.80 -Python 3.7.4 ・ Homebrew

2. Module installation

Install selenium with the command prompt.

pip install selenium

Install webdrider using Homebrew. If you do not have Homebrew, please install Homebrew first.

brew install chormedriver

3. Code creation

Import the webdriver from selenium. Import sleep that processes and stops for seconds.

form_send


from selenium import webdriver
from time import sleep

There is a module in selenium itself that waits for processing, but when I tried it, the operation after the page transition did not work, so I will stop the operation with sleep (). In the following cases, processing is stopped for 10 seconds.

sleep(10)

Launch a web browser and enter the forms URL in *** of url ='***'.

form_send


browser = webdriver.Chrome()
url = '***'
browser.get(url)

If you enter the URL of forms, you will be taken to the login screen, so check the element of the text-box where you enter the email address. Open the developer tools with ⌘ + option + i and press the mark below on the upper left to specify the element. image.png If you look it up, Microsoft forms uses the id of i0116. (It may have changed, so it is recommended to check it at the time of creation) You can specify the element from id by using browser.find_element_by_id (). You can fill in the text box with .send_keys (email). Please specify your own email. Find the button id in the same way. Click the element with .click ().

form_send


browser.find_element_by_id('i0116').send_keys(email)
browser.find_element_by_id('idSIButton9').click()
sleep(3)

Enter the password screen and login maintenance confirmation screen in the same way.

form_send


browser.find_element_by_id('i0118').send_keys(password)
browser.find_element_by_id('idSIButton9').click()
sleep(3)

browser.find_element_by_id('idSIButton9').click()
sleep(3)

Then you will be taken to the forms answer screen.

Since id is not specified in the answer part of forms, we will specify xpath.

・ If you want to press a radio button

browser.find_elements_by_xpath("//input[@value='***']")[0].click()

For ***, find the value of the radio button you want to enter with the developer tools and enter it.

・ When entering a textbox

browser.find_elements_by_xpath("//input[@class='***']")[0].click()

For ***, find the class of the radio button you want to enter with the developer tools and enter it.

In browser.find_elements_by_xpath (), the ones that apply to the specified xpath are listed and fetched, so the first is [0], the second is [1], and the third is [2]. .. Therefore, if you want to put a character in the second textbox, [0] becomes [1].

Press the submit button when you are done. Enter the code.

browser.find_element_by_xpath("//button[@title='Send']").click())

Finally close the browser tab and exit

browser.quit()

Whole source code

from selenium import webdriver
from time import sleep

email = 'Input by yourself'
password = 'Input by yourself'
url = 'Input by yourself'


browser = webdriver.Chrome()
browser.get(url)

browser.find_element_by_id('i0116').send_keys(email)
browser.find_element_by_id('idSIButton9').click()
sleep(3)

browser.find_element_by_id('i0118').send_keys(password)
browser.find_element_by_id('idSIButton9').click()
sleep(3)

browser.find_element_by_id('idSIButton9').click()
sleep(3)


#It depends on what you want to enter in forms.
browser.find_elements_by_xpath("//input[@value='Input by yourself']")[0].click()
browser.find_elements_by_xpath("//input[@value='Input by yourself']")[0].click()
sleep(3)

browser.find_element_by_xpath("//button[@title='Input by yourself']").click()
sleep(10)

browser.quit()

4. Specify the time with cron

I don't rent a rental server, I run it automatically using cron on my mac.

Unlock Security & Privacy in System Preferences and select / usr / sbin / crn from + to open it.

image.png

Open a terminal and type crontab -e. ・ When executing at 6 o'clock every day (* means to execute all)

#Minutes, hours, days, months, days of the week, python directory (be sure to enter the absolute path)
  0   6    *  *   *   python form_send

Since cron is not running in sleep mode, set it to wake up for 1 minute before that. Select a schedule from Energy Saving in System Preferences and enter the time. image.png (If you want to run it many times a day and start it only then, use pmset)

5. Postscript

I wrote an article for the first time. Writing an article took a lot of patience, but I think it was good for improving understanding and killing time. I created an automatic answer for Microsoft forms with a fairly impure motive, but I would be very happy if you could use it for a good one. Thank you for reading until the end.

Recommended Posts

How to auto-submit Microsoft Forms using python (Mac version)
How to change Python version
How to exit when using Python in Terminal (Mac)
How to install python using anaconda
How to get the Python version
How to prepare Python development environment [Mac]
How to erase Python 2.x on Mac.
How to check opencv version in python
[2020 version] How to install Python3 on AWS EC2
How to specify TLS version in python requests
How to install Python
How to install python
How to build an environment for using multiple versions of Python on Mac
How to set up a Python environment using pyenv
How to make a Python package using VS Code
How to retrieve multiple arrays using slice in python.
[Introduction to Python] How to stop the loop using break?
How to execute a command using subprocess in Python
[Introduction to Python] How to write repetitive statements using for statements
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
Tabpy 1.0 (2020-01 version) How to install
[Python] How to use list 1
How to update Python Tkinter to 8.6
Post to Twitter using Python
How to use Python argparse
Start to Selenium using python
Change python version using pyenv
Update python on Mac to 3.7-> 3.8
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
Introduction to Python (Python version APG4b)
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Using OpenCV with Python @Mac
How to use Python bytes
[Circuit x Python] How to solve circuit equations symbolically using sympy
How to transpose a 2D array using only python [Note]
How to update FC2 blog etc. using XMLRPC with python
How to find the cumulative sum / sum for each group using DataFrame in Spark [Python version]
How to write a Python class
[Python] How to FFT mp3 data
Python: How to use async with
How to rebuild python environment from pyenv on Mac environment (El Capitan)
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to get followers and followers from python using the Mastodon API
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to install OpenCV on Mac
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to get started with Python
[Python] How to import the library