Using Python with Selenium to Automate Mouse Clicks and Filling Forms | by Melvynn Fernandez | Towards Data Science

For this example, we are going to log in thru Instagram’s website app.
I actually use Python and Selenium on my daily workflow. At the company I work for we have our own web application that sends reports online. With each report we have an account.
Since we have customers onboarding each day we also need to create new accounts. The number of accounts grew exponentially as we hired more team members for sales. Creating new accounts is a very manual process, looking at a report then typing in that information. This is where I discovered Selenium.
Selenium is an automation tool for web browsers. You can use it with many different programming languages other than Python. They also have a version where you can record your process in using the browser and they are able to imitate them.
It is very useful if you have any tasks that can easily be repeated. For example at my job, I get a list of new accounts from the Customer Success team. In that file it has the account name, account ID, and address. I have to put that in manually for each new account. Selenium can fix that.
Let’s start with something simple and automate logging into Instagram. Logging into any site is basically a small form that we send over. We will fill in the “form” for credentials and then click the submit button.
Requirements:1. Python w/ selenium module2. chromedriver (special google chrome that selenium uses)
First thing first is to download and if necessary install the requirements. Assuming you have Python installed you can do a pip install on the modules. The chromedriver we need can be found here.
We can now create a new Python file. Let’s start with our imports.
from selenium import webdriver
Since we are using chromedriver as an application. We need to tell Python where it is located. I just downloaded it and left it in my downloads folder. I can set it’s location now using its path.
chromedriver_location = "/Users/Downloads/chromedriver"
Once we have the location of the chromedriver we can now use Selenium’s webdriver to call it and tell it to go to the Instagram home page.
driver = webdriver.Chrome(chromedriver_location)driver.get(‘https://www.instagram.com/')
We can try running the script now and see what we get. If it is set up correctly a new instance of Google Chrome should pop up and bring you to the site requested.
Let’s get back to our code, assuming we already have Instagram credentials. We want to tell Selenium to click on the Log in link in blue:
Selenium makes automation easy by finding elements in the web page. There are multiple ways to find a web element such as id, class name, text and so on. For this case we will use Selenium webdriver’s find_element_by_xpath.
Back to that Google Chrome window and right click on the blue Log in link. You want to click on inspect, a window should pop up with all the web elements and a highlighted row. You now want to click that highlighted row and copy the xpath.
Once that is done you should have something like this when you paste it down.
//*[@id=”react-root”]/section/main/article/div[2]/div[2]/p/a
Similarly it is the same process for the input fields for username, password, and login button.
We can go ahead and do that on the current page. We can store these xpaths as strings in our code to make it readable.
We should have three xpaths from this page and one from the initial login.
first_login = '//*[@id=”react-root”]/section/main/article/div[2]/div[2]/p/a'username_input = '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/label/input'password_input = '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/div/label/input'login_submit = '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button/div'
Now that we have the xpaths defined we can now tell Selenium webdriver to click and send some keys over for the input fields!
Let’s start with the first login again. We use find_element_by_xpath and give it the first_login variable. Then we can tell it to click.
driver.find_element_by_xpath(first_login).click()
It now brings you to the credentials page with the input fields. Similar process but now we want Selenium to fill these fields with our username and password. Selenium has another method called send_keys that makes it easy for us to automate typing. We just put a string in there and it will type for us. We can just try dummy variables, for now, to see if it is working. Also, attempt logging in knowing that we will get rejected.
driver.find_element_by_xpath(username_input).send_keys(“username”)driver.find_element_by_xpath(password_input).send_keys(“password”)driver.find_element_by_xpath(login_submit).click()
If everything is set up correctly our final code should look like this:
from selenium import webdriverchromedriver = "/Users/Downloads/chromedriver"driver = webdriver.Chrome(chromedriver)driver.get('https://www.instagram.com/')first_login = '//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a'username_input = '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/label/input'password_input = '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/div/label/input'login_submit = '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button/div'driver.find_element_by_xpath(first_login).click()driver.find_element_by_xpath(username_input).send_keys("username")driver.find_element_by_xpath(password_input).send_keys("password")driver.find_element_by_xpath(login_submit).click()
We can now run this script and see the automation in the browser. This specific script will work but get an error from Instagram because we don’t have proper credentials. Go ahead and change the username and password strings wrapped around send_keys with your own information and it should log you in successfully.
As you can tell this is just a simple instance, there are different cases for different people. The possibilities are endless.
I also provide tutoring and career guidance available here!
If you enjoy the content, please feel free to support me on Patreon!
Don’t forget to connect with me on LinkedIn if you guys have any questions, comments or concerns!