Click Buttons and Type with Selenium | Python Selenium Tutorial [Part 2]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody to another selenium tutorial in the last episode i showed you how to get started with uh chrome webdriver just opening up some urls and getting it set up however today i'm going to be showing you how to click on actual buttons using selenium webdriver with no involvement from you at all um so before you begin you might want to install this for your chrome um it's called xpath helper and it'll actually help us identify these buttons for chrome and you might want to also take a look at this w3schools.com syntax for xpath just because it'll help you understand it a little more so if you see here on the profile if we open up the xpath helper it'll open up a banner on the top and we want to select this button right here so how we want to do that is by selecting the html element for that so if you right click and do inspect or do control shift i it'll open up the developer tools panel for google there's also one for firefox as well i know and if you hover over the element that you want to click we want to click on the name so if you click there it automatically brings you to that element um in html form so we want to click on this however you don't want to be confused by accidentally clicking on say this right because even though it looks selected on this screen if we were to put that in as an xpath element it would select all of these boxes and selenium can't select objects if there's conflicts so we want something that has a unique identifier to it so there's no other objects on this page with the name loopjamestyler.github.io um so we want to use um the title as our unique identifier so to do that we want to do slash slash as a query and then you want to put the html element so span and then we're going to do open bracket at title um and we're saying that you know the the title we want that to be used as our identifier and then we're going to put luke james tyler dot github dot io and we're gonna put a close quotation marks and also another bracket and you can see here it says results one loopjamestyle.github.io and it shows up highlighted right there now say if we wanted to do say if we before how we had that um conflict say if we decided to do something that has multiple elements so if we did div um and then at class and then let's say pinned item let's just copy and paste that class name into here you could see that it's now selecting multiple items and we don't want that because it will cause an issue with selenium so let's go back to what we previously had on selecting only one item so now you're going to go back into your program and you're gonna now import what is called action chain so you're gonna do from selenium dot webdriver dot common dot action chains import and then it's going to be action chains um and this is going to allow us to double click um however we don't actually need this if we were doing single click um but we'll get that to that in a couple of minutes so let's change the url right now to my github profile and so if we just open this up as it is let's just make sure it's running okay so it's working so we could cross that off our list um and now we want to click on that repository that we were working with so if we do folder equals driver dot find and then you can just auto fill if you have um if you have pycharm but if not you're going to do find underscore element underscore by xpath um and that is what we are using and then you're gonna do slash slash and now you're gonna put in that query that we were working with before so if you haven't already you can just copy and paste it now into here um and also you're going to realize that you have to put this in quotation marks this entire query so you actually want to put whatever the text is in a different type so you want to put that in single quotes so now we have this as our folder object but we want to click it so if we do folder dot click and now if we run this i'm gonna drag it over um so it opens up on my profile and really quickly it clicks into the repository that we were working with um so awesome that works now however if we try to do that with say a double click um it wouldn't work this is only a single click so say something that is double click like this if we want to click on that image right there you know we can't do that using click so let's hover over that element in there um and so let's get the x path for this element so let's do image as our html element and then i guess we can use um alt as our unique identifier and then if we put luke james tyler um you can see it's working because it's kind of like a vague over a highlight if i unselect it you can see that it's now regular but if i complete it um it's in there so it's good that we have that result now we want to double click on that element so if we throw it in now and replace our we don't have to change anything with this so our x path is now going to be this image right here okay so now if we tried this as it is um with just the single click you're gonna see that it pro oh it actually worked but um that that was not supposed to happen um but if you wanted to do a double click you know you can't do that using a single click say if you you know you had to click on an item and it requires double click um you know it wouldn't work with just that click but we should be able to do that anyway so let's just um i'll show you how to do that so if we do now action chains um and work with that okay so now since we already have action chains um imported on the top command we can now just do action chains equals action chains and then we want to input our driver and then we'll do action chains driver dot double click and then put folder and then we also want to put dot perform at the end didn't mean to double type that um so now if we try to run this um we might just get the same result as before so yeah um actually two ended up popping up so if we do double click it um two will come up so that is a way of showing that double clicks do work um i'm trying to think of something else on that on this page that if we you know double clicked it would work okay since i want to show you the double click example um we're going to be testing this out on a different website right now one that you know if you you have to double click it to see the alert if you just right click this it'll have different items right here but we want the double click so if we now find the html element for this double click icon right here um we see it's a button and we see it's containing this text so to do that we might want to do um slash slash button and then put that open bracket and then do at and then i don't know what class name this is but it's like at on d click um double click i guess that's what it stands for and then equals uh my function open brackets and then we can see that it's now selecting this right here so we're going to use this as our target element and so if we throw this in here as well as we have to change our link now since we're using a different website so if we change the link now um and everything else will stay the same i'm gonna run it and you're gonna see it's loading up the website now and it double clicked it so there you go you have the um success saying that you double clicked that button so also in this tutorial i'm going to be showing you how to do something that's called send keys and basically what that is um is putting in text into a text box right here um so for this demonstration we're going to be using the site evernote just because they never like to have me logged in but for this we're going to need to find this element so if we go into our inspect element panel and hover over the element and as well as pulling up our xpath helper if we do slash slash um input and then we're gonna do open bracket at name and then we're going to just set that equal to username and with that i can tell that it's already selected and you can see results equals one so it's the only element using this um so that's going to be perfect for our demonstration um and now we're going to change the website actually so let's change this to evernote their login um it's a long link and then we're also going to want to delete out this part right here and we're going to copy in our new xpath um and we're going to have to change the quotation marks like we always do um so if we put that in there and now if we do folder dot send keys this now takes in a string value so let's just say luke tyler we're not going to be submitting ever anything yet um but we're just going to be inputting it for selenium so now if we open it up and run it um you can see it's senate automatically loop tyler is automatically imported you can say use a variable instead of this so put name equals luke james tyler and then just pass in instead this name um and then if we run it again and it opens up the evernote website automatically sends the keys and then say if you want to hit continue you would just do what we previously went over about selecting buttons um so that's been it for this tutorial thank you very much for watching if you found this at all helpful please leave a like and stay tuned for next episode where i'll be showing you how to use what's called headless chrome which is running this without the actual browser interface as well as downloading files using selenium um it's a really cool episode and you get a lot more in-depth experience with clicking different buttons and a workflow
Info
Channel: Stealth Rabbit
Views: 10,254
Rating: 4.878788 out of 5
Keywords: selenium, selenium python, selenium tutorial, what is selenium, selenium webdriver, chromedriver, geckodriver, Setting up Webdriver in Selenium, how to open chrome with python, selenium python tutorial for beginners, web scraping for beginners, selenium web scraping, action chains selenium, send keys selenium, RAM Rabbit, how to use selenium, how to use selenium with python, selenium install, selenium step by step, Xpath, xpath tutorial, Click Buttons and Type with Selenium
Id: JXroNn1DwGk
Channel Id: undefined
Length: 13min 21sec (801 seconds)
Published: Wed Aug 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.