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
from selenium.webdriver.common.by import By
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 all the elements of the page is loaded.
Trick 1: 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.
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
browser = webdriver.Chrome(
executable_path="C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
browser.get(website_URL)
refreshrate = int(15)
while True:
time.sleep(refreshrate)
browser.refresh()
|
Trick2: How to login on a website, here we take example of Zomato
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
browser = webdriver.Chrome(
"C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
browser.maximize_window()
time.sleep(3)
username = "test"
password = "test"
browser.find_element(By.XPATH, "//a[@id ='signin-link']").click()
time.sleep(2)
browser.find_element(By.XPATH, "//a[@id ='login-email']").click()
a = browser.find_element(By.XPATH, "//input[@id ='ld-email']")
a.send_keys(username)
b = browser.find_element(By.XPATH, "//input[@id ='ld-password']")
b.send_keys(password)
browser.find_element(By.XPATH, "//input[@id ='ld-submit-global']").click()
print('Login Successful')
browser.close()
|
Trick 3: Instagram Login automation script.
We know that Instagram is discontinuing its Legacy API from 29 June, 2020.
So it’s probably a better idea to learn automation scripting.
Python3
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
class instagramBot:
def __init__(self, username, password):
self.opts = webdriver.ChromeOptions()
self.opts.add_experimental_option("detach", True)
self.driver = webdriver.Chrome(options=self.opts)
self.username = username
self.password = password
time.sleep(2)
self.driver.find_element(
By.XPATH, "//input[@name = 'username']").send_keys(self.username)
self.driver.find_element(
By.XPATH, "//input[@name = 'password']").send_keys(self.password)
self.driver.find_element(
By.XPATH, "//div[contains(text(), 'Log In')]").click()
time.sleep(2)
self.driver.find_element(
By.XPATH, "//button[contains(text(), 'Not Now')]").click()
instagramBot('Sample Username', 'Sample Password')
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Apr, 2023
Like Article
Save Article