Python scripting is an interesting thing in Python. Automation and controlling the browser is one of the few scripting in Python. In the current section, we will find out the process of logging into a Facebook account by using Python and the power of selenium for data science.
We will use selenium for opening the site of our need and then we will inspect the elements across password box, email box and login button for finding id of them.
- By using the function find_element_by_id(), which is provided by the selenium module, we will be able to find out the required element( username box, password box, login button)
- We will also be using the send_keys() function which is provided by the selenium module. This will help us to send the data containd in the box.
- Installation of required third party modules:
Importing the required modules:
We need to import the following modules as they will be required in the function.
- Selenium: It will help in the automation of the browser.
- Time: It is there for pausing running scripts for few seconds as the browser tries detecting automation stuff when we give input too fast.
- We have to take the username and password as input from the user:
In this case, we will use the input() function and we will pass prompt messages as argument.
- Opening the browser and the required website: For opening a new window of chrome, we will useChrome(). Its object will be save in the variable named driver. Now we will use the get() function for opening Facebook website.
- Finding the elements for sending of data and sending input: We will be using the inspect element tool on the element of browser for which we want to find the id. In such a case, we need to inspect the username box, password box, and login button for finding the id. We will then use this id combined with selenium function find_element_by_id() for finding across web pages and save it in variables for using them later. We will then use the send_keys() for sending data across the elements found previously.
- Closing of browser: After following the steps given above, please quit the sessions by using driver.quit().