How to access clipboard (copy / paste) [Selenium] | LetCode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey everyone! My name is Koushik. In this video we're going to learn how to access the clipboard values from our system into a selenium script. For example, when I click on this button here so here when I click on this button, it says that the value has been copied right so here we are getting a small tool tip which says that it's copied right now if I go to my note pad and if I paste so here I am able to get this value right which is this value right so what are the values within this text box we can copy that and we can paste so that is how normally clipboards works, right? So what we are going to do now is we are going to copy this and maybe we will open this in another tab or I can juut sys out so you get the basic ideas right now let's do that in two ways one is by Java and another one is using the javascript now let us understand how this really works under the hood how does the DOM manipulates and also how we are getting this value, okay? So for that let me give you an example in this so let so here we have this value right this value is nothing but this exact value so within this box we have a value feild where we are getting this now if you notice here this is my actual button right so this button is here now in this we can see that data clipboard target it's an attribute right so in this attribute we have this has #foo that means. ID right? In CSS #means id right so here if you see id equal to foo so whenever this guy is going to click we are going to do some click action it is going to take the value from here that is it will go and check for the foo ID and it will take the corresponding value okay, so that is how in JavaScript it works. So what can we do is we can find this button first, okay, so we can find this button first then we can check which ID is located within this data clipboard target and based on that we can find the internal time what value it has so if I get this value then obviously I can open this in new tab, right? So here I'm going to create a main method. And let me load this website here. So here I'm going to say Chrome Driver driver equal to new Chrome driver so basic stuff right and here I'm going to say driver dot get to fetch the URL. And then. I'm going to find this button. I'm going to I have to click on this button, right? So, let me inspect this. So here it's image tagged on that we have about that we have button direct. So we have to click on this button, but this button doesn't have any maybe class to be also do tickets. I believe yes, so we have 34 so that's only a good idea. So, what can we do this we can write the XPath or maybe the CSS. So let's let's see how to write this CSS for this. So here it seems like we have ID here example target, so I'm going to copy this and here. After that. I want the below button right so button. So we got this now, right? So it's highlighting so obviously I was CSS is correct. So I'm going to copy this and here. I'm going to say driver dot find element by. CSS and here I am going to paste this right? Now, let's store this in a web element, so I will say button. Fine. Now what we want? So from this button, we want to get the. Attribute, right? So this data clipboard target we want this attribute, right? So I am going to copy this attribute and here I am going to say button. Dot get that review of this, okay? Now, this is going to return access string. So I can print this. So let's print and check what is giving us. So attribute. And let me run this. So the idea is very simple we are going to get the button attribute and based on that attribute we are going to find the equivalent id on the right, okay? So the element has the value which is going to be our clipboard value, right? So let me close this and here we can see we got this foo right? Now what we are going to do is we are going to find this element, okay? So here I'm driver dot find element by CSS and, Here I am. Going to pass my attribute right because if you see the attribute holds this value right has that and four right so if I go and inspect in this so here I can see that so it's belong to that button right now in this button this actually here we are getting matched but this is actually this value right so here I can check that. Okay, so it's giving us this id foo where it has this value this is what we have to get right okay, so I'm going to store this in a web element and I can say this as maybe copy or target. And from the target we want to get the value right so target. Dot get that review. And within that reboot I have to pass the value right so here I can pass the value. Which is nothing but this value right so this value is holding this URL, right so I'm going to take this value okay, so here I can store this in another string, so let me name this as value and. And let me print this out so just so value. Again, so first we will print this value to make sure we are getting the same exact what we needed then we will open this in another another tab. Okay so it ran successfully and here we can see we got this value right so this is what equivalent of this so when we click on this button, so if I click on this button, it says that copy now if I go and paste this here, so obviously it will I can open this in another tab right now, let's understand how to open this in another tab using javascript because we know that selenium four has this feature but still, Selenium three does not have this future, so I'm going to trigger the javascript code here, so let me open my inspect comes on and within the console. I'm just going to type so window. Dot open so window dot open is the method which needs a string as an argument and if I give the string here it is going to open in a new tab right so we got the value now what I'm going to do is I'm going to take the value and I'm going to pass this using execute script so that I can open a new tab here so here. I'm going to say undriver dot. Executed script and within the executed script. I have to pass the value right so here I'm going to say. Window dot open and within the open I have to pass my valuable right now. I cannot pass directly so I was obviously I have to do the string concatenation here so here I'm going to save value but if you have noticed within the double quotes we have to give again a double quotes right so I'm going to use the break single here so slash double slash mean double quotes and here I'm going to use break, right? Okay, so this for the syntax so here we are getting this value and here we are using this break so that we can have the double quotes as well now, let's try to run this. Okay so now it's open the new tab which is we are getting from the copy but now you might ask me Koushik we are clicking on the copy and we are getting the value but what you are doing is actually we are taking the value from the runtime and you are passing it but what if when I click on the button and if there is any issue on that or there is an any bug on that how do I know that right that's a valid question so doing this way? It's like technically if you know JavaScript this is how this is how under the hood it works right but as I tested we have to do in another base right so the second way might be using robot class where we can use the control using the cards we can use the control v and all those right but that's not a good idea because you know, right so whenever if some one is going to open any notepad file or any text file. So based on that current location it is going to type so which might fail our test case so the another approach is using the toolkit right so let me show you that so what I'm going to do is I'm going to just create a main method here another main method and I'm going to make this as main one. Now I'm going to run this method okay, so I'm going to use the toolkit here.... which is coming from your java.awt. And here I'm going to get the get default toolkit right now from here we can get the system clipboard side so get system clipboard and this clipboard is going to return as a clipboard as a class okay, so let me store that and this is going to clipboard okay, so we are getting this clipboard now from the clipboard we have to access the data flavor, that is another method so we have. To access the string value from all clipboard right so here I'm going to say clipboard. And here I can say get data and within the data I have to pass another data flavor okay, so here it's going to be data flavor and here we need this thing right so string data flavor is a static final variable through which I can get this right now if I store this this is going to return me as an object but here we can see that this is going to throw us an except ion. I mean compile time error which says that its need to be handled right so as of now I'm going to use try catch. You can use the throws as well, so I'm going to use the try catch here and here II'm going to print this in this side so this out data. So that's it now, let me open my notepad and I'm going to copy write something and copy so here. I'm going to copy this okay, so right click and copy and if I run this program, you will see what I have copied. Right so we got the answer like so let me show you again, so I'm going to just change this and I'm going to select and I'm going to copy and I'm going to run this program again. Okay, so we got this value right so now let's try to do this using our test case, so I'm just going to copy everything not maybe everything we just want this right so we want to click on this so let me bring this here. And here I'm going to say button dot, click so button dot click. Obviously I need to access that driver so let me copy this second pastry here instead of getting class variable and all I think is pretty much cool. Okay so we created the object and we also open this URL and also we are clicking on the button that means obviously the clipboard has some value right now based on this. I'm going to take this clipboard so using this clipboard dot get data. I'm getting the string value right now. I have to get this. Once I copy right, so I'm going to bring this down. That's absolutely fine let's be decrease the font size so that we can see okay now what we have to do is we are we are going to open this in new tab right so I'm going to copy this and here I'm going to paste this here and instead of value. I have to pass this as data right so that's it, so let me run this now. Okay, so we are now able to see it right so we open we got the value from the clipboard and we are able to open that in a new tab either you can open in a new tab or you can paste to some text editor or input box anything right so you can do that whatever you want so in this way, we can access the clipboard values right so this is a very quick demo. I got this question in Facebook. I saw that and I thought to answer it but before that someone answered from a blog which has not this implementation but kind of this simple message where they have used the robot class but we know that robot class is not that much handy when assigned that we are going to run our test case in headless mode then obviously robot class is not going to help us right so using this get system, click board and this data. Figure so obviously it is going to help us so I hope you have enjoyed the video if you have any queries you can reach through me you can comment in the video and please do like and share and if you like the video and you want to see more like this you can subscribe to my channel, so thank you guys thanks for watching see you in the next one very soon.
Info
Channel: LetCode with Koushik
Views: 15,918
Rating: undefined out of 5
Keywords: how to copy from clipboard, java selenium clipboard, copy and paste, selenium copy paste, selenium open new tab, How to access clipboard, How to access clipboard copt paste, letcode, letcode koushik, copy paste java, how to paste using selenium, robot class selenium, selenium new window, clipboard, copy paste javascript, how to open a link in new tab in selenium, how to open new tab in chrome using selenium webdriver, how to open new tab using selenium java
Id: Bt3v6ivGRxI
Channel Id: undefined
Length: 14min 0sec (840 seconds)
Published: Tue Apr 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.