0

First off, I am a Javascript developer tasked with dealing with Selenium, and I feel the pain QE goes through now lol, so my knowledge of Java is basic. I have the following DOM structure and I need to be able to find "Link Name 1" and click it. I will also have to do the same for the other links in different tests.

<div class="some-container">
  <div class="blah"></div>
  <div>
    <div>
      <div class="common-class-name">
        <a>Link Name 1</a>
      </div>
    </div>
    <div>
      <div class="common-class-name">
        <a>Link Name 2</a>
      </div>
    </div>
    <div>
      <div class="common-class-name">
        <a>Link Name 3</a>
      </div>
    </div>
  </div>
</div>
<div class="some-other-container">
  <div class="blah"></div>
  <div>
    <div>
      <div class="common-class-name">
        <a>Link Name 1</a>
      </div>
    </div>
    <div>
      <div class="common-class-name">
        <a>Link Name 2</a>
      </div>
    </div>
    <div>
      <div class="common-class-name">
        <a>Link Name 3</a>
      </div>
    </div>
  </div>
</div>

Here is one of the things I have tried, the findElementsWithWait is basically a utility function that uses findElements with a wait. This does not work.

List<WebElement> list = findElementsWithWait(By.className("common-class-name"));

for (WebElement element: list) {
  WebElement el = element.findElement(By.tagName("a"));
  String text = el.getText();
  // other code would check for the proper text and click...
}

What is the best way to get the list of a tags to be able to click them based on text value?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Aaron
  • 2,056
  • 2
  • 20
  • 42
  • As I can see from HTML sample links have different texts. Why do you want to collect them in list and then click each by individual text? – DonnyFlaw Nov 24 '20 at 23:44
  • I updated the markup. The page actually can have hundreds of these, they are tooltip flyout menus that are part of a list of items. I am able to use the search box to isolate a single item, so your idea will work, but if I could not, which is the case for other instances, I would like to know how to handle it. – Aaron Nov 24 '20 at 23:52

2 Answers2

0

Try to search each anchor node by its link text:

By.linkText("Link Name 1")

If there are multiple links with the same text and unique ancestor, then try search by XPATH

By.xpath("//div[@class='some-container']//a[.='Link Name 1']")
By.xpath("//div[@class='some-other-container']//a[.='Link Name 1']")
DonnyFlaw
  • 373
  • 8
0

To click() on the first link with text as Link Name 1 within <div class="some-container"> you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategy:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='some-container']//a[text()='Link Name 1']"))).click();
    

To create a list of the links with text as Link Name 1 you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following Locator Strategy:

  • xpath:

    List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[starts-with(@class, 'some') and contains(@class, 'container')]//a[text()='Link Name 1']"))).click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217