Selenium Python Tricks

Python Methods and Functions

What is a WebDriver?
Selenium WebDriver — it is a test automation tool. When I say automation, it means that it automates test scripts written in Selenium.

Installing Webdriver

 Chrome: https:// sites .google.com / a / chromium.org / chromedriver / downloads 

Library imported

 from selenium import webdriver import time 

(i) Selenium library:
— Used for automation.
— Webdriver Management
— Perform actions such as: element clicks, page refresh, website navigation, etc.

(ii) Time library:
-To use sleep function because selenium only works when all the elements of the page are loaded.

Trick 1: How to increase the number of views on the site?
# Note: this will not work on all websites like like YouTube. 
What we would learn is to refresh the web page over and over again after a certain period of time.

#! / usr / bin / env python

from selenium import webdriver

import time

 
# set webdriver path here may be different

brower = webdriver.Chrome (executable_path = "C: Program Files (x86) GoogleChromechromedriver.exe" )

 

website_URL = " https://www.google.co.in / "

brower.get (website_URL)

  
# After how many seconds you want to refresh the web page
# Number of web views -site if you stay there
# for a specific time
# You should understand this

refreshrate = int ( 15 )

 
# This will continue until you stop the compiler.

while True :

time.sleep (refreshrate)

brower.refresh ( )

Trick 2: How to enter the site, here we will take the example of Zomato

from selenium import webdriver

# To use the sleep function because selenium
# only works when all elements
# page loaded.

import time

# webdriver path set

browser = webdriver.Chrome ( "C: Program Files (x86) GoogleChromechromedriver.exe" )

 
# To expand set a browser window
browser.maximize_window ()

 
# zomato link set

browser.get ( ' https://www.zomato.com / ncr' )

 

time.sleep ( 3 )

# Enter your username and password here.

username = "test "

password = " test "

 

 
# login element clicked

< p> browser.find_element_by_xpath ( "// a [@id =' signin-link'] " ). click ()

time.sleep ( 2 )

 
# Login clicked

browser.find_element_by_xpath ( "// a [@id =' login-email'] " ). click ()

 
# username send

a = browser. find_element_by_xpath ( "// input [@id =' ld-email'] " )

a.send_keys (username)

 
# send password

b = browser.find_element_by_xpath ( " // input [ @id = 'ld-password']" )

b.send_keys (password)

 
# submit button pressed

browser.find_element_by_xpath ( "// input [@id =' ld-submit-global'] " ). click ()

  

print ( 'Login Successful' )

browser.close ()





Tutorials