How to extract and parse a url from an element on a page and store in a variable

I need to extract a dynamic url from a link on a page, parse a string from it, and store it in a variable in order to concatenate that string to a url I’m navigating to later in the test case. The tips I’ve found so far on how to do this haven’t helped me much, as I don’t have a development background.

Sure, no problem:

String url = WebUI.getAttribute(findTestObject('path/to/link/object'), 'href');

You would then parse the url string to get your substring.

1 Like

The ‘href’ is dynamic, though, so how would that work? It contains a user id that is different for every user. That’s the string I need to extract.

I wouldn’t know without seeing an example. You will most likely need to make clever use of the substring() method.

Here are a few screenshots showing you an example of what I’m trying to do. The first one shows the “Edit” link that contains the string I need to capture. The second shows the code behind the link. It shows the href for the link (/user/422316/edit), which contains the user ID I need to store in a variable (422316).


The brute-force way would be this:

String url = WebUI.getAttribute(findTestObject('path/to/link/object'), 'href');
String id = url.substring(6, 12);

This assumes that the first character in the id is always the sixth character, and also that the id is always EXACTLY 6 characters long.

But my question is what do I use for the ‘href’ since it’s different each time I encounter a new user? The ID is different for each user each time a new user is created.

According to your question:

… we’ve done exactly that: parsed the ID from a link on the page, and stored it in a variable. Are you asking how to do the second part?:

So I should use ‘href’ rather than the actual href in this Get Attribute statement? I thought I was supposed to put the actual href in there.

‘href’ is an attribute of the link element (the <a> tag). So when we say:

WebUI.getAttribute(findTestObject(‘path/to/link/object’), ‘href’)

we’re saying “get me the value of the href attribute for this link”. What you’re saying is that this value will change every time, depending on which user is logged in, which is fine. It will just return whatever it finds for the href attribute at the time it’s called.

Ok I understand. I was confused but I think I’ve got it. Next question is what do I use for the ‘path/to/link/object’ since the link doesn’t have an ID.

I would create a Test Object with the following xpath:


Hopefully you are familiar with Test Objects, either from using the Recorder or creating one by hand.

Should the Test Object be created as a ‘div_’ object?

Not sure what you mean. Here’s a step-by-step:

1.) Right click on the Object Repository and click New > Test Object:


2.) Name your Test Object something meaningful, like ‘editLink’, and click OK:


3.) Add an object property with name = xpath, and value = //div[@class='user-info']//a, and make sure the ‘Detect object by?’ checkbox is checked for it:

4.) Now your WebUI command would be:

WebUI.getAttribute(findTestObject('editLink'), 'href')

1 Like

Got it. That worked. Thank you for all your help and my apologies for my lack of understanding.

No problem :grin: gotta start somewhere!


Thanks @Brandon_Hein!! This works for me as well. I’m looking for this answer for a long time. Thanks again for the details steps and helpful tips that you’ve provided.

I am also looking for getting same info
Extracting all urls and savinginto csv