Selenium

 

Selenium is a great tool for browser testing, web automation, and web scraping. Selenium can control most of the modern web browsers. i.e., Firefox, Chrome, Chromium, Opera, Apple Safari. To control a browser, Selenium needs a tool called Web driver. Most of the modern browser vendors provide the Web driver software for their web browsers.

Downloading Python bindings for Selenium, Check this link: https://selenium-python.readthedocs.io/installation.html

PROGRAM 1: AMAZON LOGIN

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
web=webdriver.Chrome("D:\chromedriver")
web.get("https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_ya_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&")
time.sleep(1)

login=web.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div[1]/form/div/div/div/div[1]/input[1]")
login.send_keys("Your phone no/email")

Submit=web.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div[1]/form/div/div/div/div[2]/span/span/input")
Submit.click()

Line 1,2, and 3 all the required components from the selenium Python library.

Line 4 Creates a Chrome web driver object using the webdriver.Chrome() method and stores it in a web variable.

Line 5, web.get() method loads up amazon sign up page in a Chrome browser.

web.find_element_by_xpath pass the login selection in login variable.

login. send_keys is used to provide your phone no / email.

Submit variable is used for submit the value through the submit php page.

Save this program as AMZ_LOGIN.py.

PROGRAM 2: AMAZON SEARCH

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
web=webdriver.Chrome("D:\chromedriver")
web.get("https://www.amazon.com")

searchbox=web.find_element_by_id("twotabsearchtextbox")
searchbox.send_keys("macbook pro")

time.sleep(1)

Find=web.find_element_by_xpath("/html/body/div[1]/header/div/div[1]/div[2]/div/form/div[3]/div/span/input")
Find.click()
time.sleep(1)

In this all steps are similar as above only difference is that,

searchbox=web.find_element_by_id("twotabsearchtextbox")

using searchbox variable we can track the id of the Search Box element.

searchbox.send_keys("macbook pro")

send_keys() method is used for providing some input.

Save this program using AMZ_SRCH.py.

HOW TO GET THE XPATHS?
=> Go to the search box, Right-Click, Inspect Element, Copy as full xpath, and paste it as it is. This requires some HTML programming language.

PROGRAM 3: ADD TO CART IN AMAZON

from selenium import webdriver
web=webdriver.Chrome("D:\chromedriver")
Producturl="product link"
web.get(Producturl)
Addtocart=web.find_element_by_xpath("/html/body/div[2]/div[1]/div[7]/div[5]/div[1]/div[4]/div/div/div[1]/div/div/div[1]/div/div[2]/div/form/div/div/div[15]/div[1]/span/span/span/input")
Addtocart.click()

Same as above steps, Producturl variable stores the product link. After getting the product, Addtocart variable add this item in cart section.

Save this program as AMZ_ADD_CART.py

PROGRAM 4: INSTAGRAM LIKE

from selenium import webdriver
import time

browser=webdriver.Chrome("D:\chromedriver.exe")
browser.get("https://www.instagram.com/")
time.sleep(4)

#Login
Username=browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
Username.send_keys("Username")
time.sleep(4)

Password=browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input")
Password.send_keys("Password")
Password.submit()
time.sleep(5)

#NotNow
Notnow=browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button")
Notnow.click()
time.sleep(3)

#Notification
Noti=browser.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]")
Noti.click()
time.sleep(3)

#Logic

def firstpic():
time.sleep(2)
pic=browser.find_element_by_class_name("_9AhH0")
pic.click()

def likepic():
time.sleep(3)
like=browser.find_element_by_xpath("/html/body/div[1]/section/main/section/div/div[2]/div/article[1]/div[3]/section[1]/span[1]/button")
like.click()

firstpic()
likepic()

Line 1, and 2 are used to import Selenium. Line 4 and 5 are used to open 'Instagram' page. Line 9 and 10 used to pass Username, provide Username in the send_keys() method. Line 13 to 15, used to pass the password. After Password Submission, For NotNOW, line 19 and 20 is used. And For Notification NotNow Line 24 and 25 is used.

Creating a Function, 'firstpic()' to select first picture using class name(HTML). After selecting the class, LikePic() function likes that picture.

Calling both firstpic() and likepic() functions to execute and run.

Save this program as INS_LIKE.py

Like this, this link provides you more information about installation and usage of Selenium. Check this. https://linuxhint.com/using_selenium_firefox_driver/

After Configuration as linuxhint, simply open terminal and type ls, and type cd selenium-firefox, to enter selenium-firefox. Type ls, and show all saving programs.

Comments

Popular posts from this blog

PYTHON TRINKET

FOR LOOP WITH ZIP FUNCTION IN PYTHON

Tuple