0

This is the button of the page I need to click:

<div _ngcontent-jnk-c107="" class="btn btn-primary btn-outline" tabindex="0" ng-reflect-router-link="/opportunity/1">VIEW</div>

I can't use the text view in the locator as there are other 22 buttons in the same page with the same text.

The only difference between them is that router link, which I don't know how to call. I already tried with Link text and partial link text, with no results.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

In case ng-reflect-router-link attribute value is unique for this element you can use the following XPath locator:

//div[@ng-reflect-router-link='/opportunity/1']
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • I tried driver.findElement(By.xpath("div[@ng-reflect-router-link='/opportunity/1']")).click(); and it still says "unable to find element" – Damian Centrone Nov 15 '21 at 14:34
  • Maybe you are missing a wait / delay before this command? Try putting something like `Thread.sleep(5000);` before it and see if it resolved the issue. – Prophet Nov 15 '21 at 15:14
0

To click() on the element with text as VIEW with respect to the ng-reflect-router-link attribute you can use either of the following Locator Strategies:

  • Using cssSelector:

    driver.findElement(By.cssSelector("div.btn.btn-primary.btn-outline[ng-reflect-router-link='/opportunity/1']")).click();
    
  • Using xpath:

    driver.findElement(By.xpath("//div[@class='btn btn-primary btn-outline' and @ng-reflect-router-link='/opportunity/1']")).click();
    

However, as the element is a Angular element, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.btn.btn-primary.btn-outline[ng-reflect-router-link='/opportunity/1']"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='btn btn-primary btn-outline' and @ng-reflect-router-link='/opportunity/1']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352