Browser automation using Selenium



Mastering Selenium helps you automate your day to day tasks like managing your tweets, texting on Whatsapp and even just googling without opening a browser in just 15-30 lines of Python code. The limits of automation are endless with selenium.

Installation

1.1 Selenium bindings in Python
Selenium Python bindings provide a convenient API to access the Selenium Web Driver like Firefox, Chrome, etc.

 Pip install Selenium 

1.2 Web Drivers
Selenium requires a web driver to communicate with the selected browser. Web Drivers — it is a package for interacting with a web browser. It communicates with a web browser or remote web server over a wired protocol that is common to all. You can check and install your browser`s web drivers.

 Chrome:  https://sites.google.com/a/chromium.org/chromedriver/downloads  Firefox:  https://github.com/mozilla/geckodriver/releases  Safari:  https://webkit.org/blog/6900/webdriver-support-in-safari-10/   

Starting

from selenium import webdriver 

 
# For used sleep function because selenium
# only works when all elements
# page is loaded .

import time 

  

from selenium.webdriver.common.keys import Keys 

 
# Create a web driver instance

browser = webdriver.Firefox () 

browser.get ( ` https://www.twitter.com ` )

 
# Let`s gender The user will see and also download the element

time.sleep ( 2 )

 

login = browser.find_elements_by_xpath ( `// * [@ id =" doc "] / div [1] / div / div [1] / div [2] / a [3] ` )

 
# using a click function, which is like clicking a mouse.

login [ 0 ]. click ()

 

print ( " Loggin in Twitter " )

  

user = browser.find_elements_by_xpath ( `// * [@ id =" login-dialog-dialog "] / div [2] / div [2] / div [ 2] / form / div [1] / input` )

 
# Enter your username

user [ 0 ]. send_keys ( `USER-NAME` )

 

user = browser.find_element_by_xpath ( `// * [@ id =" login-dialog-dialog "] / div [2] / div [ 2] / div [2] / form / div [2] / input` )

 
# Reads the password from a text file, because
# storing the password in a script is just silly.

wi th open ( `test.txt` , `r` ) as myfile: 

Password = myfile.read ( ) .replace ( `` , ``)

user.send_keys (Password)

  

LOG = browser.find_elements_by_xpath ( `// * [@ id =" login -dialog-dialog "] / div [2] / div [2] / div [2] / form / input [1]` )

LOG [ 0 ]. click ()

print ( "Login Sucess full " )

time.sleep ( 5 )

 

elem = browser.find_element_by_name ( " q " )

elem.click ()
elem.clear ()

 

elem.send_keys ( "Geeks for geeks" )

 
# using keys to send special keys
elem.send_keys (Keys.RETURN) 

 

print ( "Search Sucessfull" < / code> )

 
# close browser
browser.close () 

Cutting through the code

The above script is for entering twitter and looking for geeks for geek descriptors. 
So let`s see how it works:
1. Opening the browser
2. Instantiating the browser and using the .get function to connect to the website. 
3. Find an element can be anything, find an input field or a button and use a selenium function like click (), send_keys (), etc. to interact with the element. 
4. Closing the browser

By now, you must have figured out that this automation script works in a iterative way of finding an element and interacting with it. There are various ways to find an element on a web page, you just right click and view the element and copy the element by name, css selector or xpath.

Well, in general, with this, you can create your own automated script for each individual site or a universal script for all your social networks that automates all your actions. 
There are no automation limits and the above is just an example to get you guys getting started. So happy programming!

Related posts: