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??
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();