Newbie question- How to copy the second word in a text box

Imagine a text box with the following text,

We hold these truths to be self-evident…

I would like to copy the second word which, of course, is “hold.”

Or imagine that same text box, with the following text,

When in the course of human events…

In that case, I would like to copy the second word which, you can see, is “in.”

I am sorry for asking such a simple question but hours of searching and trying different approaches has been fruitless.

I’m using Katalon Recorder in Firefox 78.0.1 on Linux.

I doubt it matters, but I can successfully move the cursor to the very beginning of the text box which contains the text in which I would like to copy the second word. However, I don’t know how to copy the second word.

I’m neither a developer nor a QA guy. I am merely a “power user”, that is, I am not very technical at all. Therefore, because “when you are hammer, every problem looks like a nail” I was thinking of a kludge, which I realize might seem ridiculous to you guys, such as…

  1. Ctrl+Right_Arrow (to move to the end of the first word)
  2. Right_Arrow (to move past the space between the first word and the second word)
  3. Shift+Ctrl+Right_Arrow (to select the second word)
  4. Ctrl+C (to copy the second word)

I don’t use Katalon Recorder, so you will have to judge if I am helpful or not, however, what I would use is the String function, split(" "). (There is a space between the quotes.) What split does is separate a string into parts. In this case, the parts are whole words.

stext = textboxtext.split(" ")[1]

Now, split starts the first word at 0, the second word would be 1. Now you have to figure out how to use it in Recorder context (if it can be).

Thanks for your suggestion. In Katalon Recorder (not Katalon Studio) I see…

setText and storeText

but I don’t see…

stext

By the way, below are a bunch of links I found which might possibly be helpful. I don’t know.

  1. Using storeValue and echo in Katalon Recorder
  2. Copy-Paste with Katalon Studio
  3. How to paste from clipboard in katalon recorder
  4. How to copy from Browser - paste into excel sheet ?
  5. How do I paste from clipboard?
  6. How to select/highlight text on a page?
  7. How to simulate ENTER Key Press in Katalon Studio?

Hi
based on katalon.com page:

open | https://www.katalon.com/
storeText | //*[@id=“page-top”]/div[6]/div[1]/div[1]/div/div/div/div/p | textFromPage
runScript | return ‘${textFromPage}’.split(’ ')[1] | secondWord
echo | secondWord

Or if You want to copy to clipboard just search google for “javascript copy to clipboard” and you will have ready to use examples and then just use one of this in Katalon Recorder with “runScript” command

Thanks for your help Piotr.

Did I follow your directions correctly? Please see…

[info] Executing: | storeText | //div[@id='search-results']/ul/li/fl-user-tile/div[3]/button | textFromPage |
[info] Store 'Hire Me' into 'textFromPage'
[info] Executing: | runScript | return ‘${textFromPage}’.split(’ ')[1] | secondWord |
[info] Expand variable 'return ‘${textFromPage}’.split(’ ')[1]' into 'return ‘Hire Me’.split(’ ')[1]'
[info] Executing: | echo | | secondWord |
[info] Time: Fri Jul 10 2020 05:48:16 GMT-0700 (Pacific Daylight Time) Timestamp: 1594385296796
[info] Test case passed

How can I cause secondWord to eventually end up in a spreadsheet?

Frankly, I have just about solved my problem—albeit inelegantly—by kludging togetherer Katalon Recorder and a couple of Python scripts (each of which calls a couple of bash scripts) which I execute with a hotkey via AutoKey. But, as I’m sure you know, remarkably kludgy solutions like this tend to break. And frankly, I barely know how to use Python.

Therefore, I’m not sure if I would be able to fix my solution if it were to break. Of course, I would rather have a more elegant solution instead of this Rube Goldberg contraption I have reluctantly fashioned out of necessity.

I suppose ideally, I’d like Katalon recorder append the value of secondWord to a text file which I could later manually import into a spreadsheet such as Google Sheets or Gnumeric.

In case you want a chuckle, here’s one of my two Python scripts…

import time
time.sleep(0.2)

times_to_repeat = 10

while times_to_repeat > 0:


    keyboard.send_keys("<tab>")
    time.sleep(0.2)

    keyboard.send_keys("<enter>")
    time.sleep(0.2)

    keyboard.send_keys("<tab>")
    time.sleep(0.2)

    keyboard.send_key('<right>', repeat=3)
    time.sleep(0.2)

    keyboard.send_keys("<shift>+<ctrl>+<right>")
    time.sleep(0.2)

    keyboard.send_keys("<shift>+<left>")
    time.sleep(0.2)

    keyboard.send_keys("<ctrl>+c")
    time.sleep(0.2)

    keyboard.send_keys("<escape>")
    time.sleep(0.2)

    window.activate('Google Chrome',switchDesktop=True)
    time.sleep(0.2)

    keyboard.send_keys("<ctrl>+v")
    time.sleep(1.0)

    keyboard.send_keys("<down>")
    time.sleep(0.2)

    window.activate('Firefox',switchDesktop=True)
    time.sleep(0.4)
    
    times_to_repeat -= 1

I recently start to learn python and i never used keybord or window like You did but it’s look pretty simple - there is not a lot of code and all… I think it should work just fine for a long time.

For storing values in file on your dekstop You have at least two more options:

  1. Use Js to create and download file with your value inside.
    pros: pretty easy to do with ready to use script from stackoverflow
    cons: after downloading you have to manually upload file on google drive or something

  2. Use katalon helper tool. More about it here: https://docs.katalon.com/katalon-recorder/docs/katalon-recorder-helper-tool.html
    Basiclly it’s small sever that listen on port 18910 so if you (from Katalon Recorder) open page like:

http://localhost:18910/execute?cmd=dir

this server will catch it and execute "dir"command in command prompt and return result in json format and content of page will chcange to that JSON response so you can then work with it in Katalon so it’s two way communication between Katalon Recorder and operating system (I use it on Windows 10 and it’s work really good). For longer command (address bar can handle about 2000 characters) i just write powershell script and I just execute this script with parameters.
from PowerShell (or Python - it’s should work too) I can do anything.

Some time ago i have even combine this two solutions. I have a lot of data to download so I can’t use them as parameters in URL for katalon helper so first i download them with JS and then i run PowerShell wich then work on this file.

You can check additional info here:

Thanks for all of that helpful information Piotr. I really appreciate it.

I recently start to learn python and i never used keybord or window like You did but it’s look pretty simple - there is not a lot of code and all…

Yes, what I wrote is very simple.

I think it should work just fine for a long time.

Thanks but it’s a kludge. I’d rather implement an elegant solution.

For storing values in file on your dekstop You have at least two more options:

  1. Use Js to create and download file with your value inside.

    A. Pros: pretty easy to do with ready to use script from stackoverflow
    B. Cons: after downloading you have to manually upload file on google drive or something

The JavaScript approach you indicatedseems promising to me because the pro you indicated seems like a big pro, yet the con you indicated seems like a trivial con. I had presumed I would need to manually import a file into a spreadsheet. Frankly, doing so would probably only take me only a couple of minutes once every week or maybe even only once a month.

What type of script should I look for on Stack Overflow?

Katalon Recorder Helper Tool

The Katalon Recorder Helper Tool requires Java Runtime Environment version 8.x be installed on my machine. I would prefer not to install JRE.

something simple like: “JS download file”, “JS create and download file” or something like that.

Does the following look like what you are thinking of?.. How to download file using JavaScript only

My code:

function saveData(file, data) {
    var fileElement = document.createElement('a');
    var json = JSON.stringify(data);
    fileElement.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(data));
    fileElement.setAttribute('download', file);
    document.body.appendChild(fileElement);
    fileElement.click()
}

I added this with “addScript” and then I prepare data and using “runScript” i run this function

Thanks for taking the time to send me the JavaScript you use. I appreciate that very much.

I added this with “addScript” and then I prepare data and using “runScript” i run this function

I understand. Thanks.