1

I have this hyper link to a document as...

<a _ngcontent-bcl-c5="" class="card-title" ng-reflect-router-link="/activityDetail,c2efa137-0cf3-" href="/activityDetail/c2efa137-0cf3-8997-bedb-94520063517b">AMA_LEARNING_AMA-2019-088</a>

I used the xpath as

By.xpath("//a[contains(@href,'activityDetail')]") (OR)
By.xpath("//a[contains(@href,'/activityDetail/')]")

Of course, it is an angular application, I am trying to use Selenium with Java. Most of the web element locators are working fine, except this one. When I tried it in the dev tools window, it correctly located 2 such elements, but when I ran the test none of them got located. Yes, I could have used ngWebDriver tool, but not many of angular attributes are present to use it, though. Any helpful suggestion, please ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
PraNuta
  • 629
  • 3
  • 12
  • 33
  • What is the error you are getting? – Appu Mistri Aug 02 '19 at 04:06
  • Check if the elements are there in the iframe. If the answer is yes, then you have to switch to frame in order to interact with them. Just try this xpath to see if the iframe is there `//a[contains(@href,'activityDetail')]/ancestor::html/..` this will take you to the iframe document if any. – supputuri Aug 02 '19 at 05:53

1 Answers1

1

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

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("AMA_LEARNING_AMA-2019-088"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.card-title[ng-reflect-router-link^='/activityDetail'][href^='/activityDetail']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='card-title' and text()='AMA_LEARNING_AMA-2019-088'][starts-with(@ng-reflect-router-link, '/activityDetail') and starts-with(@href, '/activityDetail')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Finally, it is easier than I thought it would be. Just by incorporating Thread.sleep(5000) made those two web elements located. As it is I know it is a bad practice to use implicit Thread.sleep() approach. So, I would be adopting the approach suggested by @DebanjanB with wait mechanism. – PraNuta Aug 02 '19 at 16:24