TestProject Forum

How do I validate a link on a button without clicking on it?

I have button with link to other page on it
Is there a way to check is link correct without clicking on it?
I know, there is “Get current URL” action, for that I need to click on button, wait for new tab to load, check url and close tab.
And for images, that method does not work

There might be an easier way but this is the first way I can think of. You can use ‘Is Present?’ to see if there is a button on the page with the link you are talking about. If it finds it, the test passes and if not the test will fail.

You need to have knowledge in the areas below:

  1. XPath to define that element
  2. The HTML structure on the page you’re testing




I think your locator will eventually be something like //a[contains(@href, 'mylink.com')]. This example gets any link that contains ‘mylink.com’ in it. But it doesn’t include any identifiers for a button. You need to play around with it to understand it.

1 Like

Well adding //a[contains(@href, 'mylink.com')] finds element, but there is problem if I have several elements with links ​like

  • /button_link
  • /button_link_2
  • /button_link_3

and with using XPath like //a[contains(@href, ‘/button_link’), recorder locates all 3 elements. if I add additional XPath, like //a[. = 'Button 1'] and move it up to the first position, then recorder only checks //a[. = 'Button 1'] and ignores second XPath, even if link there is not valid

Are you trying check one link or or multiple links? When I read your original post, I assumed you were only checking one button. If you are checking multiple buttons, you need to create separate ‘Is Present?’ steps for each.

  1. Is Present step for Element A (xpath 1)
  2. Is Present step for Element B (xpath 2)
  3. Is Present step for Element C (xpath 3)

You shouldn’t be adding all the xpaths under Element A. Element A should only contain locators that are related to itself so you can identify it as a unique element.

To clarify my previous post, //a[contains(@href, 'mylink.com')] was just an example that I gave to look for any link and may not specific enough. like you said, it located all 3 elements at first. I’m sure if you search around the topic of XPath, you will see things like /html/body/div/div/... which drills down further.

Another tip is to copy the xpath in the browsers dev tools and then modify it to your liking.

I have 3 elements on page

  1. Button_1 with link /button_link
  2. Button_2 with link /button_link_2
  3. Button_3 with link /button_link_3

I’m trying to check if link on “Button_1” is right.
If I’m checking if element “Button_1”, with XPath //a[contains(@href, '/button_link')], is present, test finds all 3 buttons. If I add another XPath to element “Button_1”, in recorder, for example //a[. = 'Button_1'], test only checks first XPath in element.

For example:
Element “Button_1”
First XPath - //a[contains(@href, '/button_link')], second - //a[. = 'Button_1']
Test checks first XPath, finds elements with links, that contains /button_link and says “Passed”, even if “Button_1” is not present.

Element “Button_1”
First XPath - //a[. = 'Button_1'], second - //a[contains(@href, '/button_link')]
Test checks first XPath, finds element with //a[. = 'Button_1'] and says “Passed”, even if link on “Button_1” is /bAtton_link.

Screenshot for first case

Screenshot for second case

If you understand the syntax //a[contains(@href, '/button_link')] or //a[. = 'Button_1'], you will know that it’s only identifying an anchor link anywhere on the page. It is not identifying an actual button (even though your element Name is Button_1). You need an xpath that identifies that button right? There are different ways you can do it and you can try reading XPath in Selenium : Tutorial on how find XPath | BrowserStack.

Try to experiment with xpath to understand how it works. Without seeing the page structure, it’s difficult for me to help you. I suggest that you go through XPath tutorials because I think that is what you are having trouble with.

Use the tip I gave in the previous post to understand the true xpath of your button. You can also look at How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug? - Stack Overflow

1 Like