How To Locate Element By Link Text Or Partial Link Text In Selenium WebDriver

In my previous posts what we have learn is we can Locate Element By NameLocate Element By IDLocate Element By Class Name And Locate Element By Tag Name to take any action on that element of software web application. We will look about different Selenium WebDriver software testing tool's actions and operations in my upcoming posts. Now let us learn two more element locators 1. Locate Element By Link Text and 2. Locate Element By Partial Link Text on software web application page.

Locate Element By Link Text
If your targeted element is link text then you can use by link text element locator to locate that element on software web application page. Look in to bellow given image.


My Targeted element Is Link text 'Click Here' and I wants to click on It then I can use It as bellow.

driver.findElement(By.linkText("Click Here")).click();

Locate Element By Partial Link Text
Same as Link text, We can locate element by partial link text too on software web application page. In that case we need to use By.partialLinkText at place of By.linkText as bellow.

driver.findElement(By.partialLinkText("Click ")).click();

For locating element by link text on software web application page, we need to use full word 'Click Here' but in case of locating element by partial link text, we can locate element by partial work 'Click' too.

Look in to bellow given example. I have used both the element locators to locate links.

Copy bellow given @Test method part and replace it with the @Test method part of example given on this page(Note : @Test method is marked with pink color in that example).

@Test
 public void test() 
  {
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   driver.findElement(By.linkText("Click Here")).click();//Locate element by linkText and then click on it.
   WebDriverWait wait = new WebDriverWait(driver, 15);
   wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("18:"))); //Locate element by partial linkText. 
  }

9 comments:

  1. please explain last two lines of code

    WebDriverWait wait = new WebDriverWait(driver, 15);
    wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("18:"))); //Locate element by partial linkText.
    }

    ReplyDelete
    Replies
    1. Last two lines will wait for link to become clickable.

      Delete
  2. WebDriverWait wait = new WebDriverWait(driver, 15);

    In the above code is 15 time give to wait in milliseconds??

    ReplyDelete
  3. i need to cick a link but that name is available in many place. but i when i try to locate it by path , everytime the path is getting failed.
    any other key word to click that link .

    example , i have link "new" located in two different place of a table

    ReplyDelete
    Replies
    1. you can try with dynamic xpath and dynamic css selector, so you can find it easily

      -^6years experiance in selenium.

      Delete
  4. If the link cannot be identified uniquely, then you need to look for other locators or XPATH / CSS should solve this.

    ReplyDelete
  5. Could you explain this code?
    //wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Đăng ký bằng email"))); //Locate element by partial linkText.
    }
    What this function to capture?

    Thanks,

    ReplyDelete
  6. browser.get(url)
    i = 0;
    try:
    while (True):
    print (i)
    i = i+1
    link = browser.find_element_by_link_text('View more comments')
    link.click()


    except Exception as e:

    print (e)

    pass
    how can limit to one post

    ReplyDelete