hello everyone and welcome to a brand new series on selenium in Python.selenium is this is a framework that allows us to interact with HTML elements. from any website so rather than just. scraping information which we can do with selenium we can grab information from web pages we and also interact with it so we could, in theory, make things. like BOTS we can have you know like and automated the script that goes through and test different aspects of our website
it's actually quite useful and it's.pretty easy to get set up which we're gonna be doing in this video so some common examples of things you could do with selenium just basic movements are like drag-and-drop clicking a button filling in a form entering something in a search field grabbing data from some specific tags finding elements in the source code reading the entire page source code pretty much anything you.want to do with a web page, you can automate that with this Python selenium.stuff the scripting which I'm going to be showing you which I think is pretty cool so we need to get into the setup. here I will say if you appreciate this type of content where this artical.
to start with selenium with python
what we need to do is we need to install selenium so I'm gonna assume you have.Python installed for your machine.
a code editor that we can write Python code for you
is make sure we have Python installed
some version of that and then we need to
get-pip working which I'll talk about in
a second install the selenium module and
Chrome web driver
if you need to more explanation about click this link
the first step is to get pip working so pip
stands for package installer Python and many of the popular Python modules are installed and kind of configured throughout that or through pip now pip is installed and comes by default with a Python installation so you do have pip on your computer sometimes it can be a little bit finicky though and not work earlier you can't access it so I'm gonna go through those mistakes quickly.
first if you're on Windows open up a command prompt window if you're on Mac or Linux open up your terminal window and if you're on Windows so you're gonna type pip and hit enter if you're Linux or Mac you're gonna type pip and hit enter it may be PIP you might be pipe try both of them if you're on Mac or Linux but the idea is that we hopefully want to get some output that looks something like this it doesn't have to be identical but something like usage pip command options you just don't
want to get this error pip is not recognized as an internal or external command so if one of these two work for pip or pip good pretty much ready
Linux windows whatever then you're gonna need to configure pip and make sure that that's set up properly on your system now some of the common reasons for this
it's not on the system path that's why
you can't access it you do how PIP you
then come back here and start following along so once pip is working we have everything good all we're gonna do is in our terminal or command prompt window
type pip install selenium
install selenium that if you're working in a virtual the environment you're gonna have to make sure it's installed in that virtual the environment I assume you guys know how to configure that if you are using a virtual environment but for most of us is just gonna be pip install selenium and assuming that works fine
we're ready to go selenium is installed now it's just time to get the webdriver so again make sure this work we can test if this is working by going into our Python window or python script and typing selenium so import selenium
if we run that and we don't get any errors then we're good to go you know the program is fine if you do get an
error then you got to make sure that you have this package installed in the right python interpreter which is a common error that I can't really go over here
because that's a whole video on its own but anyways once selenium is up and running I'm gonna assume you guys are at that stage now we're actually gonna have to go to this website which you can see
the Chrome web driver so for this tutorial series I'm gonna be using Google Chrome as our main interface using Google Chrome so that's gonna mean download the Google Chrome browser if you don't have it and then come to this webpage which there'll be a link to in the description and we need to download the Chrome web the driver associated with our google chrome installation so these
are two different things the Chrome
browser is different than the Chrome webdriver you need both of them for this to work so first chrome second browser our driver sir so what we need to do is
figure out what version of Google Chrome
we're using and then download the
appropriate web driver version so to
find that out if you go to google chrome you can hit these little dots at the top right hand corner go to help and then
about Google Chrome and it will show you right here if your chrome version number and then download the correct
version from this page so you can see
that it says if you're using version download this so will download a but if you're using an older version you can scroll down and they have the older versions here all of that it's probably worth it just to update.
we'll do win so download that this is gonna be a zip the folder I'm gonna put this in my downloads folder notice I already have that downloaded I shouldn't take very long and once we have the downloaded so you've moved past that
need to extract that and place that in a specific location on our operating system so make sure you guys are following
this is hopefully save you if your headaches and some selenium errors extract this file so just double click into it if you're on Windows you know uncompress on Mac whatever it is you need to do and copy this file so I'm gonna do control C or actually control +X
because we're about to move it to an easier location to access so I suggest move it in your project directory.
where we want chrome driver to sit so you can place this anywhere you want just make sure you know where you place because we're gonna have to reference the path to that file so the path to the file can be found by simply.
the browser it's what's able to perform the actions. using selenium we're gonna do is say to the driver to run the first step to open browser
its gonna pick the browser pride by your initiation.so this is the case I've been recommending chrome so we're gonna use Chrome with a either you can use Firefox
do in here is simply put the path variable that we defined below so we're going to say the web browser we want to use as a chrome and the web driver that is for this browser is located at the path
webdriver Chrome path and now what we can actually do is open up a web site
from selenium import webdriver
driver= webdriver.Chrome(r"C:\seleium tutorials\chromedriver.exe")
that's how we open up a web page that's how we get to what we want to do and I'll show you really quickly how we caninteract with the web page just to give you a little bit more in this first
we can access any web page like this in here I'm not going to describe more about selenium commands because I'm already cover this on different tutorials you can visit using this ink to that artical.
driver.get("https://techbitezzz.blogspot.com/2020/08/general-electric-rolls-royce.html")
if we want to close a web page what we can do is use driver close what this will do is close the current tab so that may not close the entire browser window it will just close the tab but if you want to quit the
entire browser then you can do quit now obviously if you only have one tab maybe you run close it closes that tab it will close the browser but if you want to make sure you're closing the entire browser you can run quit so if I run this now let me give it a second you'll notice right as it gets to the web sit it just goes ahead and quits.
compleate code
from selenium import webdriver
import time
driver = webdriver.Chrome(r"C:\Users\HP\PycharmProjects\seleium tutorials\chromedriver.exe")
driver.get("https://techbitezzz.blogspot.com/2020/08/general-electric-rolls-royce.html")
driver.maximize_window()
time.sleep(5)
title = driver.title
print(title)
driver.quit()
and will see you guys in another artical.
No comments:
Post a Comment