Change language

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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically