How to select text by class name and copy

I want to find text using class name (“text”) and copy the text without mouse on Chrome. How can I do that? Can I find text and select it by using body id?

Hey Timo,

Please provide a working URL we can test with.

-Chris

If the element with a class of "text" is the first element with that class on the page, this will return the text:

document.querySelector('*[class="text"]').textContent

Put that in a Execute a JavaScript in Browser actions (KM Wiki), and set the output to "System Clipboard"

This is unlikely to work, unless the class "text" is unique, or the element with it is always first on the page. As @ccstone says, you need to provide us with the URL to get a better suggestion.

1 Like

That is what I want to do without mouse movements. I now how to do everything else than select and copy the result.

(Bible verse is on clipboard so KM only paste it.)

Right-click at the top of the area you want to copy, and select "Inspect".
Then, expand the html element, and copy from one element above to one major element below.
Make sure the element tag name, class, and id (if any) are visible.
A screenshot of just that region is probably the easiest to do.

Given that, we should be able to formulate a brief JavaScript to copy the text.

Thank you. Is that what you need? :slight_smile:

This copy all text, if I first click somewhere on the text. I don't actually know what that code means, I just copy it from this forum :smiley: .

(function () {
var oSeln = window.getSelection(),
nodeTable = document.evaluate(
'./ancestor-or-self::table',
oSeln.anchorNode,
null, 0, 0
).iterateNext(),
rngDoc = nodeTable ?
document.createRange() : null;

if (nodeTable) {
oSeln.removeAllRanges();
rngDoc.selectNode(nodeTable);
oSeln.addRange(rngDoc);
}
})();

So if I can select text (onload) somehow without using mouse it would be nice.

Hey Timo,

Alright — appended is a working macro for you to play with. it's pretty basic, but quite a lot of customization is possible.

Copy your Bible verse to the clipboard, and run the macro.

I've used front-browser actions in everything but the New Google Chrome Tab action, so you can easily change it to work with Safari if desired.

I would have used all front-browser actions, except that you specified Chrome — and I couldn't be certain you'd activate the macro only when Chrome was the frontmost browser.

You might possibly have to (or want to) adjust the pauses in the macro.


For future reference — if at all possible please provide an actual URL to any web page you have questions about. Your PNG has the URL, but a prospective helper has to jump through hoops to get there.

Answering questions about working with web pages is often hellishly difficult when you can't directly test the page in question.

-Chris


Lookup Bible Text on Raamattu.kmmacros (7.2 KB)

2 Likes

Hi Chris,

It works! Thank you a lot for helping me!

This helps me so much :smiley:

I have a macro like that:
First I write bible were to Word (where I do my notes in bible school) like that: Math. 1:5-9
And when I set the cursor front of verse and press hot key

  1. macro copy verse
  2. open url where is bible verse search
  3. Search verse
  4. Copy verse text
  5. Paste it on Word
  6. And finally make it in the right form

It takes 2 second and works perfectly without mouse movements.

(sorry my english, hope that you understand)

Thank you!

-Timo

Hey Timo,

Yeah! And you bet.

Super! Isn't automation great?

No problems — you made yourself understood.

-Chris

Could I piggyback off of this and ask how to save each found element into a separate variable instead of combining them into a list?

I am actually just working with 2 found elements right now, so even if I could just return the first result, save it to variable, and then return the 2nd and save it to a variable, that would be enough.

If by "found element" you mean the text that @ccstone's script returns for each found element, then yes, this is easily done.

The script returns a text list, with each found text on a separate line.
So all you need to do is use a For Each action (KM Wiki) with a Lines In collection (KM Wiki), where the collection is the KM Variable returned by the script.

In the Actions inside of the For Each Action you can process each line separately, including assigning the line to a variable.
But why assign it to a variable? Perhaps you can process the line immediately.

Perhaps if you provided us with the details of your workflow, we could offer some specific suggestions.

Thanks! That got it working for me.

I am assigning a variable because I want to use it later in some calculations and such.

I appreciate the help. I have a couple of other issues that are coming up for me, but they are beyond this thread, so I think I will start a new post for them instead.

1 Like