Get desired value from <div> tag based on sometext using Inject Js Script activity

Hi everyone,

Currently, I have a html code of a web page which contains some “div” tag as below:

I want to check whether the “div” tag content contains SomeText or not. If yes, I want to get the id value in “a” tag from that “div” tag.
I intend to use Inject Js Script to do that but I don’t know how to get that.
Please help me if you have any solution for this problem.

Thanks and best Regards,
Hoang Anh.

Hello @anhth15,

Please could you include few screen shots detailing the problem.

Also, could you please elaborate what exactly you need help with.

Warm regards

Hi @prassin6,

I just updated my question as above.
Please let me know if you have any further question.

Thanks and best Regards,
Hoang Anh.

Hi @anhth15

I had a similar issue, but I found a solution in this post How to get dynamic url. There you can find examples of js. code that I used to make my issue work.

Hope this helps :slight_smile:

1 Like

Hi @nbjerke,

Thanks you so much for your link.
But I’m not good at js. So I don’t know how to apply it for my case.
Please help me if you have any idea for that.

Thanks and Best Regards,
Hoang Anh.

Well, this is not really a JavaScript forum but you need to identify where you find the element in the tag.

In my example I needed to extract an url from a button on google. I found the found the value using this code:

document.getElementsByClassName("_ldf")[0].children[0].href

So my code in the inject JS Script activity looked liked this:

function(e) {
 var url_link = document.getElementsByClassName("_ldf")[0].children[0].href;
 return url_link;
};

Which returns the url as a string.

You need to change the value after the “=” sign in order to get your value. I would suggest playing around with the console in your web browser to try to find the element you are looking for.

Edit: I tried your HTML code:
14

So if thats the text you need you can just replace the “document.getElements…” in the picture above, after the “=” sign and you should be able to extract the element :slight_smile:

4 Likes

Hi @nbjerke,

I really appreciate your help.
Finally, I figured out the js code to achieve my requirement as below:
function(e) {
var dDivs = document.getElementsByTagName(“div”);
var desiredText = “sometext2”;
var output = “not found”;
for (var i = 0; i < dDivs.length; i++) {
if (dDivs[i].innerText.indexOf(desiredText) !== -1) {
output = dDivs[i].getElementsByTagName(“a”)[0].getElementsByTagName(“img”)[0].id
}
}
return output;
};

Thanks and Best Regards,
Hoang Anh.

1 Like