How to write code for linkText for the html code

54 Views Asked by At

I am new to selenium and trying to write linktext code for the html code

<a href="repopulateUser.jsp?authAuthSessionId=7DD74346650647B7BA9ED08C1ABAE66D&amp;roleId=bc95f1f2-9ccd-11e8-9a37-0050568817ef&amp;ROLE=Programmer&amp;ROLECODE=PROGRAMMER&amp;roleGrpId=f4c11ca7-9c1b-11e8-9a37-0050568817ef&amp;moduleId=cb475927-7eb1-11e8-97d0-0050568817ef&amp;studyId=null" class="labels">Programmer</a>

Code trial:

driver.findElement(By.linkText("Programmer")).click();   

the code above is not working, is something wrong?

1

There are 1 best solutions below

0
On

linkText should work if the element with the text Programmer is uniquely identified within the HTML. Still as the element is a JavaScript enabled element so to click() on the element you need to use elementToBeClickable() and you can use the following Locator Strategy:

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Programmer"))).click();
    

Alternative

As an alternative you can also use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.labels[href^='repopulateUser']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='labels' and starts-with(@href, 'repopulateUser')][text()='Programmer']"))).click();