0

Table with links

Hi All,

I have the following table with links that I need to select. In this specific example I need to select the DIY Payroll but sometimes this can change its position within the table. The current xpath is:

.//*[@id='catalog-category-div-1']/table/tbody/tr/td1/ul/li[4]/a

So I do a:

By.xpath(".//*[@id='catalog-category-div-1']/table/tbody/tr/td[1]/ul/li[4]/a").click()

But the problem is here is that it can change position where it can be in td[2] or td[3] and li[n'th postion]

Can I have selenium go through the table and click on it based on text. Will the By.linktext() work here ?

1
  • had you tried with By.linktext()? Performing any action on any element on webpage can be tricky as it may depend on multi layer elements, like here you are trying to reach the <a> element and click that link but you have to try in various way to make that happen, locating that element is one part! Commented Sep 16, 2016 at 19:22

4 Answers 4

1

You can use the following codes these codes will handle the dynamic changes.

  1. You can use linkText() method as follows:

driver.findElement(By.linkText("DIY Payroll")).click();

  1. If you want to use xpath then you can use following code.

    driver.findElement(By.xpath(.//a[contains(text(),'DIY Payroll')).click();

If you need any more clarification you are welcome :)

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest that you try By.linkText() or By.partialLinkText(). It will locate an A tag that contains the desired text.

driver.findElement(By.linkText("DIY Payroll")).click();

A couple issues you might run into:

  1. The link text may exist more than once on the page. In this case, find an element that's easy to find (e.g. by id) that is a parent of only the link you want and then search from that element.

    driver.findElement(By.id("someId")).findElement(By.linkText("DIY Payroll")).click();
    
  2. The A tag may contain extra spaces, other characters, be capitalized, etc. In these case, you'll just have to try using .partialLinkText() or trial and error.

  3. In some cases I've seen a link that isn't an A tag or contains additional tags inside. In this case, you're going to have to find another method to locate the text like XPath.

Comments

0

You should use a CSS selector for this case: Can you try:

By.CssSelector("a.browse-catalog-categories-link")

1 Comment

Hello Moe, if I was to select a particular link based on the text ..How do I do that ?
0

You can use XPath to do this. //a will select all 'a' tags. The part inside of the square brackets will select everything with text "DIY Payroll". Combined together you get the desired solution. //a[contains(text(),'DIY Payroll')]

1 Comment

Code only answers are frowned upon on SO. Please spend a minute explaining why and how this code solves the problem asked so that it's more useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.