I am actually writing a windows script which on clicking will open my default web browser , open a page , enter my login details and then submit the form.
All I could do so far was open the browser with this:
explorer http:\\outlook.com\website.com
Since I could not find any resource which tells how to do this, I looked at python scripting and found two libs ie. urllib
and webbrowser
. I am know that urllib
is used for scraping websites. But I am not sure how to simulate the same in a web browser. The webbrowser
library has only commands to open the page in a new tab etc.
Please tell me how I can achieve this task either using python or windows script.
EDIT: I have my username and password saved in my browser so that it auto fills. I just want to simulate the 'ENTER' key press.
IEC
For me the easiest to use option for controlling a browser from Python is IEC. I find very easy to use for simple things like you describe.
IEC is a python library designed to help you automate and control an Internet Explorer window. You can use this library to navigate to web pages, read the values of various HTML elements, set the values of checkboxes, text boxes, radio buttons etc., click on buttons and submit forms.
Typical uses for this library include:
Writing test scripts to check your web application automatically.
Managing an online account automatically.
Automatically logging and downloading webmail.
Monitoring web based applications periodically for failure.
Selenium
If you'd rather avoid Internet Exporer (and who wouldn't?) then you can use selenium
. To install it do:
$ pip install -U selenium
There are details for how to login to a site with selenium
using Chrome on this question. The important part is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = 'C:\\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
browser.get('http:\\outlook.com\website.com')
username = selenium.find_element_by_id("username")
password = selenium.find_element_by_id("password")
username.send_keys("YourUsername")
password.send_keys("Pa55worD")
selenium.find_element_by_name("submit").click()
All the previous answers are correct here is an example for Chrome and Facebook. You can use the same code but need to change the elements it's looking for and of course you have to give it, correct url, user name and password
Note: this was test on Macbook
download the selenium driver by:
cd /tmp
wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_mac64.zip
unzip chromedriver_mac64.zip -d /usr/local/bin
The code that does the work is below and again make sure you change the url, elements it looks for and user name and password. ( assumptions, open a file name called login.py and copy and paste the below code in it and then make the changes)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome()
driver.get("https://en-gb.facebook.com/login/")
elem = driver.find_element_by_name("email")
elem.clear()
elem.send_keys("YOUR_USER_NAME")
pas = driver.find_element_by_name("pass")
pas.clear()
pas.send_keys("YOUR_PASSWORD")
pas.send_keys(Keys.RETURN)
sleep(100)
driver.close()
run it:
python login.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With