Selenium and Netbeans. Copy string to clipboard, paste into browser possible?

393 views
Skip to first unread message

Simon

unread,
Oct 3, 2015, 11:50:25 PM10/3/15
to Selenium Users
Hi,

I am iterating through a list of webpage URLs.

On each iteration I visit a different webpage URL.

On each page visited I am trying to check if a word exists in the page title,and if it does I want to add a string to the clipboard, so that the user can manually paste  (CTRL+V) the string into the active Selenium browser window.

Code:
String pageTitle = driver.getTitle();

while(true)
{
       
if(pageTitle.contains("ABC"))
       
{
           
System.out.println("The page title contains ABC, copying ABC text to clipboard");
           
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
           
Transferable transferable = new StringSelection("ABC"));
            clipboard
.setContents(transferable, null);

       
}
       
else if(pageTitle.contains("DEF"))
       
{
           
System.out.println("The page title contains DEF, copying DEF text to clipboard");
           
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
           
Transferable transferable = new StringSelection((String) tmp.get("DEF");
            clipboard
.setContents(transferable, null);
       
}
       
else
       
{
           
System.out.println("Neither ABC or DEF found in page title");
       
}
}



When I run the Java program however after the first iteration/page visit the text isn't being copied into the clipboard, it is blank on iteration number 2+.

Can anyone advise me why the clipboard is blank on iteration 2+? Instead of it containing the assigned string?

Many thanks for any help!

Krishnan Mahadevan

unread,
Oct 4, 2015, 12:11:14 AM10/4/15
to Selenium Users
Simon,

Can you please help elaborate as to what do you mean by 

>>On each page visited I am trying to check if a word exists in the page title,and if it does I want to add a string to the clipboard, so that the user can manually paste  (CTRL+V) the string into the active Selenium browser window.

If this is a text box in the window, why can't you just make use of sendKeys() wherein you are sending the contents of a String variable, which contains whatever you need to be appending ? Wouldn't that be a lot more easier than take the ClipBoard route ?



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/8f04e67f-58b7-475b-a3b6-466a7ab906d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guillaume.fo...@nexonia.com

unread,
Oct 5, 2015, 11:36:33 AM10/5/15
to Selenium Users
I think you can also use WebElement's sendKeys() to type a real CTRL+V by passing the SYNCHRONOUS IDLE character (Unicode 0016) in parameter, although I never tried it myself.

guillaume.fo...@nexonia.com

unread,
Oct 5, 2015, 2:28:11 PM10/5/15
to Selenium Users
To add to my last post, using Keys.chord() is probably a cleaner way to do it.
Reply all
Reply to author
Forward
0 new messages