Advanced Python Programming: Browser Automation with Selenium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what is up avi here and today I'm gonna be teaching you how to use selenium in python let's get started now if you guys have seen my GUI automation with paya degree video selenium works very similar except it's just a lot more effective and what it does 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 this video is gonna be a very brief introduction to selenium we're gonna cover how to install it how to click and how to enter text three very basic commands with selenium hopefully with the end of this video you guys will have a basic understanding of how selenium works first thing first is installing selenium what you guys going to be doing is you're gonna go to pycharm 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 lowercase this a bit install package then you should go ahead and have selenium installed package selenium installed successfully however if the if that did not work and you got an error go to your terminal I'm gonna go ahead and close this let me refresh you're gonna say sudo sudo is very important here pip 3 or PIP depending on whatever version of Python you have normally pip should work install selenium so that is the first task is gonna ask for your password you're gonna go ahead and put in your password and then after you do that it's gonna say it's already been old or disabled whatever requirement already satisfying once you have that done the second thing is to go ahead and install a webdriver selenium needs a webdriver to work with your browser know I have one in Seoul already but I'm gonna go ahead and show you how to install your very own what you're gonna do is you're gonna open a new tab in your Chrome or any browser that you're using and search for just selenium okay the link that you're gonna click is the first link selenium HQ org inside of this we're gonna 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 with it now I'm using Google Chrome guys as you can see so I'm gonna install the Google Chrome driver if you guys do Mozilla opera edge whatever just installed whichever driver you need go ahead select it if you scroll down latest release chromedriver 2.34 awesome job now we see over here at latest release chromedriver 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 I'm on a Mac so I'm going to go ahead and select this and it's gonna install in my downloads folder now I already have my chrome driver right over here so I'm not gonna worry about it now as you guys do this I'm gonna go ahead and just pitch something that I've been working on I do have a Python horse coming up it's all about automation it's gonna cover selenium web scraping debugging regular expressions excel word manipulation a whole bunch of stuff I hope you guys are excited I'll definitely let you guys know when it is live but definitely stay tuned for a brand new Python course coming out soon anyways I'm back all right so we have selenium installed that was our driver go ahead and unzip it here is our chrome driver 11.5 MB fantastic we're gonna go back to PyCharm and if you go to your python consult i'm gonna go ahead and expand this if you run from selenium import webdriver if this command works you know that selenium has been installed all right so 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 we're gonna run is going to be setting up our browser so we're gonna go ahead and say browser is equal to webdriver whoops webdriver dot your actual web browser so we installed it for Chrome guys I'm going to say webdriver chrome inside the brackets if you go ahead and run this it works for some people but inside the brackets I think you have to specify the path of your webdriver so if you're in a Mac guys you're gonna say users slash your user name slash downloads slash chrome driver again if you're on a Windows machine you're probably gonna have something like C colon double backslash something double backslash double backslash 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 guys you should see an chrome window up here at the very bottom of your screen you guys can't see it because I set the dimensions of my Camtasia a bit different but go ahead and just see this 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 we're gonna go ahead and say browser browser dot and then we're gonna go ahead and say get passing a URL the URL I'm actually in a pass in is going to be the selenium HQ org URL so browser dog get pass an HTTP colon while cab WWE neom HQ or awesome hit enter and immediately we're gonna see um give it a second it's this one right over here selenium web browser has been open I'm gonna close that in the background to see this side by side guys I'm just gonna go ahead and minimize this hopefully that makes it easy for you guys to take a look at what's going on and you guys can see the live results as it happens let me go ahead and minimize this as well fantastic so this is our window guys we have our code on our left and the window on our right anything we write over here will automatically update on the right hand side now the next thing that we're gonna be working on guys is finding elements on the browser so if you go ahead and say browser da find element browser dot whoops whoops whoops browser dot find there are multiple different find element functions you can do by ID by class name by link text by partial link text stuff like that we're gonna go ahead and 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 webdriver object we're gonna go ahead and store that so let's go ahead and get that back again browser to find element by link text download and we're gonna go ahead and say LM is equal to browser to find element by link text download fantastic so this will go ahead and 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 can also click on that let's see we want to click on the download button so we already have the element over here we just have to call LM dot click 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 very straightforward the last thing that we're covering this video guys is searching how do we put in tags into a fuel as you guys can see over here this is our search box we're going to try to figure out how to put some text inside of this so let's go ahead and select this right click and inspect we're trying to look at the property or the 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 ID of Q so now we have the search bar and to enter text into any sort of input or text field you have to use the command the command is going to be give me one second over here sendkeys search bar dot send keys will allow us to populate that text field with whatever we want so I'm going to go ahead and search for just download something simple hit enter and immediately download appears so sendkeys allows us to simulate the keyword typing click simulates a user clicking if you guys remember my Python GUI automation video we're going to the exact same thing with 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 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 1x think guys that extra thing is going to be from selenium from selenium webdriver common keys we're gonna import keys okay 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 um I accidentally screwed up there guys there's not supposed to be quotation marks it's just supposed to be keys not enter my bad but if you go ahead and hit enter like this the 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 webpages I can fill out forms I can click on elements I can fill up text fields stuff like that and this video 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 the click function and we understood how to send text or put text and fields by using the send keys function now all the special Keys like up-down left-right Delete 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 don't forget to like share and subscribe and I'll see you in the next video
Info
Channel: TheCodex
Views: 322,668
Rating: 4.8993149 out of 5
Keywords: Selenium, Python, Browser Automation, selenium webdriver, selenium tutorial for beginners, selenium tutorial, selenium automation, seleium automation with python, browser automation python, selenium python, python selenium, python automation, python web automation, web automation python, python browser automation, python chrome automation, web browser automation, selenium tutorial for beginner, selenium webdriver tutorial, selenium tutorials, advanced python
Id: GJjMjB3rkJM
Channel Id: undefined
Length: 10min 41sec (641 seconds)
Published: Wed Dec 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.