TestProject Forum
Powered by leading experts in the test automation community

Copy JQueryUI Tooltip text to clipboard in Java using Selenium

I am trying to copy the contents (text) of a JQueryUI tooltip widget. (https://jqueryui.com/resources/demos/tooltip/default.html) via Selenium for Java.

A simple WebElement.GetText() does not work since (i gather) it is not treated as a regular tooltip. Nor have i had any success in retrieving the text via the JQueryUI API e.g. : $('#age').tooltip('option', 'content') or copying with JavaScript’s document.execCommand("copy")

So i would like to simulate a copy to clipboard : tooltip.sendKeys(Keys.CONTROL, "c"); but i need my automation to be cross-platform. In Mac OS it would be cmd+C which isn’t working from what i gather around stackoverflow.

I found this method of copying to clipboard :

StringSelection stringSelection = new StringSelection("test"); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(stringSelection, null);
But this requires me to produce the string myself (the “test” string), whereas i don’t have it yet. i need something to do the actual copying to clipboard. Is there any way to do this at the system level in Java? or alternatively another way to do this ?

Hey haim,

you could try using JQuery selectors and JavaScript Executor for selenium to retrieve the current value :

first you will need to ‘inject’ a custom attribute to your tooltip element, in order to be able to reach it afterwards via JQuery selector :

JavascriptExecutor jsDriver = (JavascriptExecutor) this.getDriver(); jsDriver.executeScript("arguments[0].setAttribute('MyElement', 'true')", tooltipElement);

now you can retrieve that value using your custom attribute within a JQuery selector :

String value = (String) jsDriver.executeScript("return $('[myelement~=\"true\"]').val();");

Thanks for the help Ruhit! that did the trick.