Selenium Python Tricks

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

What is webdriver?
Selenium WebDriver is an automation testing tool. When I say automation, it means it automates test scripts written in Selenium.

Webdriver Install

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

Library Imported

from selenium import webdriver
import time

(i) Selenium library:
– Used for Automation
– Control Webdriver
– Perform actions like – element clicks, refresh page, goto website link, etc

(ii) Time library:
-For using sleep function because selenium works only when the all the elements of the page is loaded.

Trick1: How to increase view count on a website?
#Note: This will not work on all websites, like youtube.
What we would be learning is to refresh the webpage again and again after a particular interval of time.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr / bin / env python
from selenium import webdriver
import time
  
# set webdriver path here it may vary
brower = webdriver.Chrome(executable_path ="C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
  
brower.get(website_URL)
  
# After how many seconds you want to refresh the webpage
# Few website count view if you stay there
# for a particular time
# you have to figure that out
refreshrate = int(15)
  
# This would keep running until you stop the compiler.
while True:
    time.sleep(refreshrate)
    brower.refresh()

chevron_right


Trick2: How to login on a website, here we take example of Zomato

filter_none

edit
close

play_arrow

link
brightness_4
code

from selenium import webdriver
# For using sleep function because selenium
# works only when the all the elements of the
# page is loaded.
import time
# webdriver path set
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
  
# To maximize the browser window
browser.maximize_window()
  
# zomato link set
browser.get('https://www.zomato.com / ncr')
  
time.sleep(3)
# Enter your user name and password here.
username = "test"
password = "test"
  
  
# signin element clicked
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)
  
# password send
b = browser.find_element_by_xpath("//input[@id ='ld-password']")
b.send_keys(password)
  
# submit button clicked
browser.find_element_by_xpath("//input[@id ='ld-submit-global']").click()
  
print('Login Successful')
browser.close()

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.