6

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.

3
  • A similar question was responded here: stackoverflow.com/questions/14445208/… Commented Dec 21, 2015 at 10:30
  • Think that is close to what I want. But I don't get the part of the cookies. I just want to access the fields and hit 'login'. I also want this to be universal. So people with some other browser as their default browser can also make use of this script. Commented Dec 21, 2015 at 10:40
  • 1
    Possible duplicate of Python simulate keydown. If you want, later, to add more than a simple keypress look at pyautogui Commented Dec 21, 2015 at 10:47

4 Answers 4

6

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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

  1. 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
    
  2. 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()
    
  3. run it:

    python login.py

enter image description here

Comments

0

selenium is a tool designed to test websites, so it is able to manage a large variety of browsers.

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import time

browser = webdriver.Firefox()
browser.get("http:\\outlook.com\website.com") 
time.sleep(10) 
username = browser.find_element_by_id("user_input_id") 
password = browser.find_element_by_id("password_input_id")
username.send_keys("your_username") 
password.send_keys("your_password") 
login_attempt = browser.find_element_by_xpath("//*[@type='submit']")
login_attempt.submit()

To retrieve your default browser name you can use webbrowser and create a mapping between webbrowser class type and selenium browser driver.

browser = webbrowser.get()
print browser.__class__

Comments

0

u'\ue007' is an enter code in selenium.webdriver.common.keys library. You might try to press "enter" after you sent your username and password.

username = browser.find_element_by_id("user_input_id")   
username.send_keys("myUserName") username.send_keys(u'\ue007') 
password = browser.find_element_by_id("password_input_id")  
password.send_keys("myPassword")  password.send_keys(u'\ue007') 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.