Selenium locator contains text/ID and Selenium Alternative in 2018

Update: This post is updated by adding location by ID and how to locate element ID, XPATH, CSS by using selenium 'successor' Katalon to easy create your test scripts.

If you want to locate elements using only part of its value you have several options: ends-with(), begins-with(), contains() and case insensitive search. You can two options of writing it:

  • Java selenium locate element by ends-with(), begins-with(), contains()
driver.findElement(By.xpath("//*[contains(@id, 'fancyword')]"));

Depending on your configuration ends-with() and begins-with() could not work. The reason is because they are part of xpath 2.0 but older browsers generally only support 1.0. So in this case you can use only contains() or to do:

  • Java selenium locate element by:
  • $ - end - note that it should be escaped
  • ^ - begin
      • contains

In the example below we are locating elements by class and word: fancyword:

driver.findElement(By.cssSelector("[class\$='fancyword']"));   //class ends-with  
driver.findElement(By.cssSelector("[class^='fancyword']"));   // class begins-with 
driver.findElement(By.cssSelector("[class*='fancyword']"));    //contains fancyword
driver.findElement(By.cssSelector("[class*='fancyword' i]"));    //contains case insensitive

If you want to select with selenium by element id then you can use:


How to locate UI Element ID

In the past there was Selenium IDE extension for Mozilla Firefox. Now in 2018 it's deprecated for the new versions and you can choose between several alternatives. I prefer to use Katalon extention which is doing almost the same. Below you can find picture of the application:


How to use Katolon - selenium IDE alternative in 2018

  • You need to add it to firefox
  • Then start Recording
  • Execute your test cases
  • Export the script to any language that you like.

Related Article