Write Selenium Script in Python I

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to talk about writing selenium scripts in Python now the first step in writing a selenium script in Python is to start the session and you can do that with this command driver is equal to web driver. Firefox and this starts a Firefox session next you can ask the web driver to take some action on the browser and this could be something like driver. getet and then you enter URL which in this case is ADM lucid.com after that you can request information from the browser for example there's titles subtitles different text text boxes and so on one example is you can say that the title is equal to driver. title which will return the title of the web page after that the depending on what you want to test you can establish different waiting strategies and an example of waiting strategy is driver. implicitly wait 0.5 seconds and so you can have different types of weight strategies for example explicit weights implicit weights and we'll cover this later on in video next another thing you can do is you can find an element for example you can use uh driver. find element and you can have by name or by ID uh and you can locate that element for example on this we have driver. find element and we have it by name you can also take actions on those elements for example you can say send keys and you can type in selenium and then send that in that element you can also request element information for example you can say that the text is equal to message. text for different elements and you can have a specification of what the message Mage is and then you can try to retrieve what the text of that message is and finally to end your selenium script session in Python you have to end the session which is just driver. quit so right now I have my project from previously open and right now in this project if you go to our previous video this is where I left off now in today's video what I'm going to do is I'm going to add some different types of functions in our python project to test this web page and I'm going to show you the web page first that we're going to be testing so it's this web page right here this is the web page we're going to be testing and we're going to play around with all of these different web elements for example this text area this text box check boxes radio boxes upload uploading files here and then these buttons and so on so let me refresh this page so this is what we're going to be working with and so let's begin so first thing I'm going to do is I'm going to create a new fun a new class in here or a new file a python one so I'm going to go here right click go to new I'm going to click Python and again remember we have to start it with test uncore in order for pest to work and this time I'm going to call it selenium commands uh it's going to be a python file so I'll click that and then go ahead and press enter now to begin this project uh or this file I'm going to start it the same way so I'm going to go ahead and I'm going to copy some code over actually uh so I'm gonna go ahead and paste that over oops copy this over so the first thing I do again is I import selenium especially it's web driver and then I import pest and this time instead of putting this py test fixture into the conf test file that we had previously I'm actually just going to keep it in this uh file so that you can see it all together and so in this first function uh this P test fixture function what I'm basically doing is I'm creating a fires spox instance and I'm passing it over into the later functions and you see that I'm basically specifying the web driver as Firefox and then I'm maximizing the window and then yeah so I've commented out the driver. quit right here and the reason I commented out driver. quit is because um I want to keep the window open so you can see what's going on instead of it closing the window right away so that's our first function now let's begin uh creating some functions that we can use to test different things in the Firefox so uh in our URL so the first thing I'm going to do is I'm actually going to go ahead and I'm going to create one called test button so F test button be a Firefox so press enter and the nice thing about using pycharm is it gives you a bunch of different suggestions on what to uh what you should write in your code so you can autofill it can debug for you and so on and first thing I'm going to do is I'm going to do firefox.you URL right here and I'm going go ahead and paste it as a string after that what I'm going to do is I'm actually going to tell it to Firefox do implicitly wait and I'm going to set as 10 seconds and what this does is it basically opens up the browser waits for a bit so that if there's any JavaScript uh elements on the web page it can load that and then we work with it and what I'm going to do is I'm actually going to specify that the title is equal to Firefox do title and I want to assert that uh the title I want to assert that the title is equal to adum Lucid so actually this should be like this because if we go back to our web page uh we inspect the elements on this web page we should be able to find the ti will up here ADM Lucid like that so actually what I'm I'm missing is I'm missing a space right here and um so that's something I'm missing and there we go so ad and L it okay now after that what I'm going to do is I'm going to specify button 4 is equal to Firefox doind element and I'm going to say it's bu uh we're going to use buy. ID and then the value of that ID is going to be button and then what we're going to tell it to do is we're going to say button 4. click and this will click the button and we have that right now um and let me go back to our web page right here and let me go to button 4 right click inspect uh and we see that its value is button four for itself for its ID so by ID this value is button four so basically that's what we have we're using an ID and that the ID value is button four and let me save this and actually I'm going to go ahead and run this one function right here so run this test and it opened up my other window but I dragged it over and then click button for and this popped up button has been disabled and you might wonder why did that pop up well it's because on our website we coded it so that when you click the button this popup will pop up so that's normal what we expected to happen will happen but you notice that there was an alert that popped up and so if we want to make it so that it accepts the alert what we can do is we can say Firefox do switch switch to uh alert. accept and now we can run this again again pull this over and it basically accepts theer instantly and so we don't see the L there anymore and yeah so that's what happened and I actually want to show you something else um if I scroll to yeah basically yeah yeah if I scroll here there's nothing saying that it failed because we asserted that title is equal to that and that is true so that's something else okay so let me write another function uh this time I'm going to write another function and this function I'm going to say I'm going to call it def test click button after valid textbox and we're going to say okay so we have that we specify what button one is and then we're going to say that if example button one in button one dot yeah uncore attribute name is equal to Value if this then what we're going to do is we're going to print Buton one dot is enabled and then what we to do is but one click afterwards and now I'm going to go ahead and run this so basically what I'm saying is if this buttton one on this value so this is basically the text on the web page right here so if I go to here it says button one right here right spec inspect value button one right there so that's the text so if it's equal to this then we'll say that it is enabled we'll print whether it is enabled or not the button and then we'll click it and then we'll see what happens so that pull it over and we see the button one has been disabled because we clicked it and what we also see is if I scroll down or scroll here um it says true and that's because the button is enabled so the button is enabled now what I'm going to do is I'm going to add another statement and instead of using is enabled what I'm going to use instead this time is is displayed go ahead and come down here I'm going to print is button one displayed it over it's right here and then I scroll down so this first one is true and it actually says that the test failed so let me see what happened if I go to here value unexpected alert message dismiss user prompt um and if I go keep going down I'm going to open up this a little bit bigger so we can see a little bit clear go all the way down and it basically says that unexpected alert present exception and essentially what that is saying is that we didn't close the alert after button was clicked so what I can actually do is I can uh take this right here copy that and then paste it over here and then basically we accept the alert after we click button one and then we'll check the status so that alert was causing some issues with checking the status so go ahead and run that again and I drag my window over accepts status and then it's like this and now it passes so now it's good so now first it says true that the button is enabled after we click it and accept the alert uh the button is still displayed on web page so that is the second thing that we wrote today let's write another function uh this time instead of writing the whole function out I'm just going to paste it over so it's a little bit faster now that we have a general idea of what's going on go ahead and paste this with buttons uh this time we're going to this URL again and we're finding button two and then we're basically saying that we're trying to display whether the button is enabled and whether it is displayed and we're checking for those statuses so go ahead and run this again and we see that the button is both enabled and display so that's good okay let's run another uh let's create another function uh this time we're going to deal with text bosses so I'm not going to copy and paste so I'm going to say definition I'm going to create a new definition our new function test and this time what we're going to do is we're going to text the text box and then we're going to use Firefox sorry thing to pass into it firefox.you boxes we're going to make our first variable T box do Firefox doind element and we're going to do buy isal to buy. ID and then we're going to do value is equal to text one the next thing we're going to do is textbox one uh do clear and then textbox one. send keys and we're basically going to type um welcome to selum python automation test okay and now that we've written that um we're going to go ahead and run this basically we're finding that text box we're clearing anything in that text box and then we're setting this key and again this text box one is basically just this area right here I inspect that it says text one is the ID and that's what we do text one so go ahead and run this I drag it over and it basically types in what we wanted to type in okay so I'm going go ahead and close this and let's write some more functions to deal with uh text boxes text areas and so on the next function we're going to deal with is one dealing with a text area and essentially it's the same thing as a text box except for it's a bigger area to type and it's this one right here so again let's go here and we're going to write um we're going to write Dev uh test uh text area uh no we're going to pass Firefox and I'm going to copy and paste this time uh to save us some time because it's relatively the same EX exact concept so go ahead and paste right here we're basically going there finding the text area this time we're using CSS selector that's something you have to notice and in order to get the CSS selector again same thing uh since uh this time we're working with text area 4 if I go to here text area 4 right here I right click inspect uh and then it's right here it's open I can actually get text area 4 uh by um going here right clicking this again and then copying and going to CSS selector right here so copy selector if I paste this right here it's that uh but I'm going to go ahead and cancel that so text area four and that's what we have right here but again we're going to clear that and we're going to send s and this time what we're going to I'm actually going to specify s is equal to um I'm going to type I'm going to copy some stuff over so we can just paste it in there I'm just going to copy this and just paste this right here turn tool whatever uh let's run this and see how that works try it over again and yeah so it pastes right here draging over we see that selenium was originally developed by just hugin and yeah that's exactly what we want nice so that's text areas and text boxes now let's work with some check boxes and stuff so I'm going to write a function basically to check a box on our web page so testore checkbox Firefox and I'm going to type Firefox doget and this time it's going to be our same URL copy that over actually I'm just to copy this whole line copy that over and then I'm going to tell it to wait five SEC 10 seconds again so implicitly wait 10 seconds and now I'm going to say that the checkbox 2 checkbox two I'm going to say that it is Firefox doind element by is by. name instead this time and I'm going to say that the value for this is check box two and then what I'm going to do is if checkbox 2. isore selected go ahead and print the checkbox has been selected or actually I uh I I actually I'm not going to do this right now what I'm going to do first is I'm just going to say checkbox 2. click and so first let us just click the checkbox to begin with so uh go ahead and run this this should essentially just click the check boox right now we're using by name by the way so it should pop up y pop up some window and then it clicks the checkbox I drag this over right here so it didn't click the checkbox so now we're going to go to and try and debug this so why did I not click the checkbox well let's see let's see why so if I drag all the way down to here the bottom of the error message element not interactable exception so if I go to here checkbox 2 is right here and if I see what how we specified It checkbox 2 maybe we got the wrong locator so that's something that common that's really common so I'm going to expect that again go to here and go ahead and by name so name is checkbox two I'm going to go ahead and try to copy its name actually I'm just going to see so it's by name checkbox 2 checkbox and so what we have is again we have by name and then our value is checkbox to so that looks good uh and then we have checkbox 2 is equal to do click and we implicitly wait and so one possibility of uh something that might have went wrong is that the check box was already uh checked so let me see um so we have it like this so let me try to run this again see how this goes drag it over it's still not working so what I'm actually going to do is I'm going to write uh I'm going go ahead and go to the web page and I'm going to copy I'm going expect this and I'm going to copy the um I'm going to copy the selector and uh let me paste the selector right here uh not the selector uh the xths I'm going to copy the XPath and I'm going to go ahead and copy X paath and then I'm going to paste it to xath right here or maybe not xath uh I think what we'll do is we'll copy the selector so CSS selector and then I'm going to go ahead and paste that right here okay so I actually looked a little bit more into this and I finally figured out why it was causing this issue when I was testing and not when I was implementing and so the reason that I kept getting this error that the checkbox 2 was not working was simply because when I was dragging the window over to show you the window I was actually minimizing it and by minimizing it I was not displaying the checkbox and so it's saying that the check box I couldn't click so if I go ahead and run this again but this time I don't drag over the window and I just let it stay on the other side then you see it passes and if I drag this over you can see that it clicks the checkbox already and so that's some nuances that you should uh think about in your own code as well uh now let me move on and I'm going to do a few more things and then we'll be done with this video so the next thing I'm going to do is I'm actually just going to paste over a function because it's a little bit longer and it's just for a simple uh simple concept so go ahead and paste this function over essentially what I'm doing is I'm doing firefox.exe and see if it's uh selected if it's not selected I'll select it and then uh I'll print else the check boox has been selected and so that's essentially what I'll do is just to check if the check boox is selected and which time it was s like so go ahead and run this I'm not going to drrive the window over again because I have to keep it maximized on the other side but it clicks a uh thing and then now I'm going to scroll down it says check box has been selected second time and so basically if it's selected then it'll say it's already selected so it's not so then I'll click C it and then the second time through uh it'll just basically go this one will be false so this one will be true so say that it was selected the second time just to show you that we are actually checking checkbox okay next I'm going to do what the final function that we're going to write and this one is Def test file upload and this is with Firefox and then I'm going to say Firefox doget so I'm actually going to copy over some code just so that you see what's going on because it's not that difficult it's just a lot to write so we're going to go to this URL we're going to find the file upload one file upload two on our web page which is basically just these choose file buttons and then we're going to send these uh locations and stuff of our files go ahead and run that and I'm not going to drag over in the beginning but I'll show you what it looks like in the end so it's done now so go ahead and drag it over we see that we have the web driver here and the slum here uploaded and yeah so that is basic introduction that we have for uh element uh interacting with different web elements using selenium in uh python if you found this video helpful please give this video a like and comment uh or like And subscribe to our Channel and yeah we look forward to seeing you next time thank you
Info
Channel: Test Automation 101
Views: 1,036
Rating: undefined out of 5
Keywords: Selenium Python Tutorial, Automated Testing, Python Selenium Setup, Python Programming, QA Automation, Selenium Testing, Python Scripting
Id: 7-mrMZ4-LS4
Channel Id: undefined
Length: 26min 25sec (1585 seconds)
Published: Mon Apr 29 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.