Automate Your Life! Coding With Python

by hgtech in Circuits > Computers

1426 Views, 11 Favorites, 0 Comments

Automate Your Life! Coding With Python

Thumnail10.jpg

Sometimes in life there are some tasks on your computer that are easy, like logging into your emails, just to name one, but they can be easier with automation in Python. Python is a coding language that is very similar to English and so it isn't as scary as other languages. When I first started coding I was trying to learn C++ which is very complicated compared to Python. And trying to learn C++ straight off without any prior knowledge can be a bit tricky. So I am here today to show you Python and how easy it is. By the time you have finished you should be able to automate anything. Have fun!

If at any time you are struggling I have a video that explains all the things in this!

Link : Python Automation Tutorial

Supplies

Things you need:

  • Web driver :
  • Selenium (Install through Pycharm.)

Also a basic understanding of Python. This isn't such a need but it would help!

Coding the Automation!

Importing selenium and first script.png
Opening the login page.png

With all that out of the way lets start coding!

First import selenium web drive, add the location of the webdriver, and also import time. (You will see why soon) :

code:

from selenium import webdriver
import time
browser = webdriver.Chrome(" ")

(If you are using a different browser change Chrome to whatever it is)

Now in between the (" ") input where the Webdriver is. For windows: C:\\ The location or Mac: /The location.

Mine is:

C:\\Users\HGTec\Downloads\chromedriver_win32\chromedriver.exe

Lets get coding!

In this code we are going to be opening instructables website and logging on! You can modify a few thing to login in you emails, or website like that.

copy the script in the image about and I will explain what things do here.

Import time ( I use this because sometimes the browser takes time to load and if it isn't fully loaded it will output an error saying it cannot find the thing you are looking for)

Username = "Your Username!" (This will be stored for later in the code, but put your username in here)
password = "YourPassword!" #same as username but instead put your password in
browser.get(" https://www.instructables.com/")  # This will open whatever url you put in
time.sleep(2) # this will stop the script for 2 seconds
Login_BTN = browser.Find_Element_By_Class_Name("Login-link")  '''To find the name of the button inspect the page. There is a video up to about this.'''
Login_BTN.click() 

(What ever you set Login_BTN to it will click so if you set that to the contest page, it will click that)

When it is run it will open a browser and click on the login page link.

Coding With Automation! Part 2

inputting your password.png

Now that we can open the login page lets get it to input the username and password. Remember how you set the username and password in the first part. we will use that now!

Again i have inspected the page to find how to work out what the name of the button is. I also have included a video in on how to do this on the last part. (Link is in the introduction :) )

Sendkeys is basically saying that we want to send the keys to this box. We will send the username into the username box and send the password into the password box.

Now when you run the script it should open chrome, Go to the Instructables website, go to the login page and login. On the video mine does not work as I haven't entered in my real username and password.

If you change 2 or 3 things in this code you will be able to login into any other website.

If you have any problems go to the link at the start of the Instructable and go to my Youtube channel :)