0

I need to select a specific text from a DIV, here is the DIV source:

<html>
<div class="roamingHostIdContainer ng-binding">
                                        Host ID: 3K9X-Q8LD-6AX6-3UGP-UL5B-YE3Z-UWCD-DGDU-AB8Y-FJD2-7W97-A63J-RVZA
                                    </div>
</html>

as you can see, the div has a bit too many spaces, back to the point, the thing is I need to select the ID value and the copy it.

My starting point was this question: How to manipulate user selected text using webdriver? then I moved to this: How to move cursor in Selenium Webdriver

I believe I can do it with a javascript executor but i'm a bit lost on how to use it, my idea is to create an element just with the "Host ID: " text and another one just with the RVZA text, but then I realize I can't create an element based only in that text, (can I?) because both elements will be the same element

So if any of you could guide me on the right path i'll appreciate it

Community
  • 1
  • 1
Lukas Quatro
  • 41
  • 4
  • 11

2 Answers2

0

This has nothing to do with Selenium, this is purely a Java String manipulation problem.

String myText = driver.findElement(By.className("roamingHostIdContainer")).getText();
String myIdx = myText.indexOf("ID:");
String myId = myText.substring(myIdx + 4).trim();

driver.findElement(some-other-area).sednKeys(myId);
SiKing
  • 10,003
  • 10
  • 39
  • 90
  • Hi SiKing, I know I can get the text by using the getText method, but the test case specifies the text must be selected and then copied to another textarea – Lukas Quatro Jul 21 '14 at 20:43
  • You can use `sendKeys()`. Or are you explicitly interested in Ctrl-C Ctrl-V key combinations? And why? – SiKing Jul 21 '14 at 20:47
  • I'm interested in Ctrl+C, Ctrl+V, because we need to verify the text can be copied from the app and pasted into any text editor – Lukas Quatro Jul 22 '14 at 00:01
  • You realize that is a functionality provided by the operating system, and not your application, right? – SiKing Jul 22 '14 at 15:42
  • By functionality provided by the OS, you mean to copy the text and paste it and any text editor or to the action of doing ctrl+c/v? – Lukas Quatro Jul 22 '14 at 15:49
  • Both I guess. It is actually a little application in your Desktop Environment https://en.wikipedia.org/wiki/Desktop_environment called Clipboard https://en.wikipedia.org/wiki/Clipboard_%28software%29 . – SiKing Jul 22 '14 at 16:22
  • so if I copy the text with ctrl+c, can I access the clipboard with this: http://stackoverflow.com/questions/7105778/java-get-pure-text-from-clipboard ? – Lukas Quatro Jul 22 '14 at 17:00
0

You can also do this by :

element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
element.sendKeys(Keys.chord(Keys.CONTROL, "c"));
element2.sendKeys(Keys.chord(Keys.CONTROL, "v"));

Maybe this is what you are looking for.

Vivek Singh
  • 3,641
  • 2
  • 22
  • 27