Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them.
In this particular article, we will see how to log in to the Gmail account using Python and the power of selenium.
Selenium automates and controls browsers and it’s activity. We can code in our way to control browser tasks with the help of selenium. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can be automated as well. As you learn more it’s so much fun to see things happening automatically and saving time in doing useless tasks again and again.
We use selenium here to open the site of our requirement (in this case Gmail) and there we inspect elements across email box, password box, and Next button to find the Xpath of them.
- Using find_element_by_xpath() function provided by selenium module, we can find the required element (username box, password box, Next button)
- Using send_keys() function, provided by selenium module, we will send the data into the box.
- Using click() function, provided selenium module, we make a click on the current element.
- .get() attribute allows us to open a webpage
- .implicitly_wait() allows us to wait till the page loads
Installing third party modules required
- Additional Requirement : geckodriver for firefox and chromedriver for chrome
Importing necessary modules
Selenium : to automate browser
Taking username and password as input from user
Using input() function and passing prompt message as argument.
Opening browser and required website
webdriver.Chrome() will open new window of chrome. We will save it’s object in variable named driver.
Now using get function we will open up the Gmail website.
Finding element for sending data and Sending input
Use inspect element tool on the element of browser of which you want to find id. In this case we will inspect username box, password box, Next button to find their Xpath. And then use this Xpath combining with selenium function find_element_by_xpath() to find it across web page and save it in variables for later use. Then by using send_keys() we will send data across the elements found previously.then by using click() we will click the element found.
Note: Here driver is the name of variable you chose for webdriver.Chrome().
This is a simple program that can login to your gmail account. Best case the account is not secure or an organisation mail-id.
Worst case if you have a gmail account which is enabled two-step or two-factor authentication.
Step 1: Get the mail-id and password
Step 2: Open the gmail login page
Step 3: Enter the gmail-id and click next button
Step 4: Enter the password and click next button
Step 5: Print success or failed and End
See how such a concise piece of code can automate things for you.
We can also enter the password without displaying it on screen, for security purpose. For that we have to include one more module called getpass. Now with just one change in input statement of the password we can input password without displaying it on screen.
Getpass prompts the user for a password without echoing. Basically it lets you enter the password without showing it on the screen.
Similarly you can also automate many other things like twitter login, tweeting, Gmail logout, and much more.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.