2

In my project I use findElement(By.linkText("")).click(); but in the web page I'm using, many links have the same name(btw I can use By.linktext only) so i want to know it its possible to navigate among the links which have the same name

Also when i use List<WebElement> h=driver.findElements(By.linkText("Years")); and print it:

[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],

update: In the page im working on, every 2nd link text with the same name is where i would like to go to, also there isnt any difference in those links except their description eg: link 1: Years ABC(description)

link 2: Years XYZ(description) can i use the description as a condition in any way??

2 Answers 2

1

If you observe the output:

[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],...]

All the elements are having the linkText as Years. So apperantly you can should be able to use:

List<WebElement> h=driver.findElements(By.linkText("Years"));

But, invoking click() on certain elements at times changes the DOM Tree and the saved elements within the list may turn into stale elements.

In these cases, printing the link names may fail as well as assertions.

Hence as per the best practices, you may like to invoke click() on the specific linkTexts as the necessity arises inducing WebDriverWait. As an example:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_css"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
    
Sign up to request clarification or add additional context in comments.

3 Comments

In the page im working on, every 2nd link text with the same name is where i would like to go to, also there isnt any difference in those links except their description eg: link 1: Years ABC(description) link 2: Years XYZ(description) can i use the description as a condition in any way??
@SCS Yes in that case you need to locate the <a> tags with texts as Years uniquely with respect to the description text. But that sounds to be a completely different issue all together. Can you raise a new question as per your new requirement? Stackoverflow contributors will be happy to help you out.
That might be a problem cause the content of the site is generated using script according to what i searched in the search engine Im not very familiar with javascript and how they generate search results which kind of brings me back to my original question or is there any other solution for this kind of purpose??thx also i only see a red flag beside your answer
0

so i couldn't find the solution to my original question, but as for the updated question i just iterated to the second element of the list i got using: List h=driver.findElements(By.linkText("Years")); h.get(1).click(); although it would be much efficient if i could get the destination URL of all the links generated by the search engine

Comments

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.