sel beginner
sel beginner

Reputation: 1

How to locate element in selenium for href

I am trying to locate the element for this:

<a ng-if="showLink &amp;&amp; customer.partnerType == 2 &amp;&amp; customer.isDirectCustomer" class="cp_text_link ng-binding ng-scope" ng-href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&amp;orgName=Attunix" href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&amp;orgName=ABC">
  ABC
</a>

I tried the XPath and CssSelector but its unable to locate element. Can someone pls help me locate the element TIA

Upvotes: 0

Views: 75

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193088

The desired element is an Angular element so to locate and invoke click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Java based Locator Strategies:

  • partialLinkText:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("ABC"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cp_text_link.ng-binding.ng-scope[ng-href*='orgId'][href$='ABC']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='cp_text_link ng-binding ng-scope' and contains(@ng-href, 'orgId')][contains(@href, 'ABC')]"))).click();
    

Upvotes: 0

Simply use text:

driver.findElement(By.xpath("//a[contains(text(),'abc')]"));

Upvotes: 2

Padma Anbalagan
Padma Anbalagan

Reputation: 17

Try using class driver.findElement(By.cssSelector("a.cp_text_link"))

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168002

It is quite hard to come up with an exact locator without seeing the full code of the page, you're trying to automate.

From what I can see so far it makes sense to stick to ABC text so try the following:

  1. Partial Link Text

    driver.findElement(By.partialLinkText("ABC"));
    
  2. Or the equivalent XPath

    driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));
    

Upvotes: 1

Related Questions