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.
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
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. 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()
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()
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.
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.
(If you want to run it many times a day and start it only then, use pmset
)
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