Search code examples
selenium-webdriverxpathcase-insensitive

Confusion about translate() function while using it for xpath case insensitivity


I am automating a page using selenium with java and trying to use a case insensitive xpath with the help of translate function as follows.

driver.findElement(By.xpath("//a[contains(translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'tools')]")).click();

'tools' text exists on the page as 'Tools'. [T as caps]

Now my question is,

  • What does '.,' means in the above code?
  • Using 'tools' in place of '.,' gives all the //a links. Reason?
  • Whenever I use 'Tools' instead of 'tools' in the above code, it does not work.

Someone help me here.

image 1 image 2


Solution

  • Now my question is,

    • What does '.,' means in the above code?
    • Using 'tools' in place of '.,' gives all the //a links. Reason?
    • Whenever I use 'Tools' instead of 'tools' in the above code, it does not work.

    The dot step is an abbreviated syntax, from the specs:

    . selects the context node

    Because it's used as parameter for a function that expects a string, will be casted by the means of string() function.

    The string 'tools' always contains the string 'tools', thus you are not filtering any selected a element when you used instead of .

    In the other hand, any lowercase string will never contain the string 'Tools', so you won't be able to select anything.