How to use selenium in python let's get started now selenium is a module in Python that lets you automate scripts do a whole bunch
of stuff with your web browser and honestly it is fascinating and very brief
introduction to selenium
I will cover how to install it, how to click and how to
enter text three very basic commands with selenium hopefully you guys will have
a basic understanding of how selenium works.
First thing is installing selenium what you guys going to be
doing is you're preferences project interpreter and then go ahead and add in
selenium again this might work I've had a couple of errors with this but if you
go ahead and just click selenium I'll lower case this a bit install package
then you should go ahead and have selenium installed package selenium installed
successfully.
however if that did not
work and you got an error go to your terminal and close it then refresh you're say
is very important here pip 3or PIP depending on whatever version of Python you
have normally pip should work install selenium so that is the first task is to ask
your password go ahead and put in your password and then after you do that it
says already been old or disabled whatever requirement already satisfying once
you have that done second thing is to install a web driver selenium needs a web
driver to work with your browser
how to install your very own
open a new tab in your Chrome or any browser that you're
using and search for just selenium and click on the first link selenium HQ or inside
of this go to download and then scroll down for something known as third-party
browser drivers okay every browser that we have we need a driver to use
selenium.
install latest release chrome driver now we see over here at
latest release chrome driver 2.34 go ahead and choose whichever version you're
using Linux Mac Windows doesn't really matter or actually it does depending on
the platform that you have
it's all about automation
and selenium web scraping debugging regular expressions excel word manipulation
a whole bunch of .we have installed selenium that was our driver go ahead and
unzip it go back to PyCharm and if you go to your python consult and expand
this if you run from selenium import web driver if this command works you know
that selenium has been installed
Go ahead and run that if it works fantastic if it doesn't
Stack Overflow is your best help alright so make sure this works make sure you
have chrome drivers sitting somewhere that you know the second thing or the
second sort of function to be setting up our browser so go ahead and say
browser is equal to web driver
It works for some people but inside the brackets I think you
have to specify the path of your web driver so if you're in a Mac say users
slash your user name slash downloads slash chrome driver again if you're on a Windows
machine you're probably have something like C colon double backslash something
double back slash double back slash remember guys windows as double backslash
Mac is forward slash command V let's get it back hit enter as soon as you run
this command you should see an chrome window up here at the very bottom of your
screen chrome is being controlled by automated test software so this tells you
that selenium is working this is our browser now if you want to open a link
with selenium say browser dot and then say
get passing a URL the URL I'm actually in a pass going to be the selenium HQorg URL so browser
dog get pass an HTTP colon while cab
So if you go and say browser dash find element browser there
are multiple different find element functions you can do by ID by class name by
link text by partial link text stuff. use a link text function to get the link
we're talking about over here download so go ahead and pass in download that
returns us a selenium web driver object store that browser to find element by
link text download and say LM is equal to browser to find element by link text
download fantasticso this will store this download object inside of our LM variable.
Now there's multiple different things we can do you can
access different attributes of the LM property you can say LM text you can say
LM dot maybe get an attribute LM get attribute we want the xref of it what's
the actual link we get it right over here you can access various actually boots
like that or you LM not click there we go downloads appear fantastic so that is the
simplest way to click you find an element you specify it you say LM not click
and selenium will click on that object let's say we want to go back to projects
if you want to go back to projects will say LM is equal to browser now find
element by link text pass in projects and then when I say LM dot click there we
go and we're back to projects fantastic so as you can see it's very straight forward
the last thing
how do we put in tags into a fuel as you guys can see search
box we're going to try to figure out how to put some text inside of this so
let's select this right click and inspect we're trying to look at the property
orther ID of this so as you can see it's an input type is equal to text ID is
equal to Q once we have the ID name we just have to access it so search bar is
equal to browser find element by ID Q command is going to be give me one second over here send keys search bar
dot send keys will allow us to populate that text field with whatever we want
So search for just download something simple hit enter and immediately
download appears so send keys allows us to simulate the keyword typing click
simulates a user clicking Pi auto GUI except with selenium the better thing is
we can access specific HTML elements and we don't have to worry about the
X&Y coordinate of various things so this is a lot better for automation in browsers now once we have
this entered let's say we want to go ahead and hit the enter key we can say search
bar dot send keys we need to access the specific special key of enter to do
that we need to import Aicloudnet
Extra thing is going to be from selenium from selenium web driver
common keys import keys once we have the special keys imported we can say
search bar dot send keys and then pass in keys dot enter fantastic as you can
see we got keys that enter key will be simulated and it searches with Google custom
search awesome so that is selenium in a nutshell guys I know I might have
rushed this a bit but hopefully all this made sense selenium again allows for automation in web browsers what that
means is I can run Python scripts I can go to web pages I can fill out forms I
can click on elements I can fill up text fields stuff here we will cover three
main things we understood how to find elements using the find element function
we understand how to click on elements by using escape all of them can
be accessed by importing this and just saying keys are up keeps keys are down keys
that escape stuff like that anyways fantastic job guys thanks so much for
listening hopefully selenium made sense to you it's a lot of fun play around
with and write your own scripts if you have any questions let me know in comment
section
Comments
Post a Comment
Please do not enter any spam links