2

I'm building a roll-up for Selenium using the following Xpath:

//a[contains(@title, "(' + args.Code + ')")]

//a[@title=\'' + args.Code + '\']

However I need this argument to be case insensitive so that whoever runs my script cannot mess up by adding an uppercase or lowercase letter.

It is important to mention that the key/property Code will change depending on what the user will write at the beginning of the roll-up.

I have been trying to find a solution but haven't find any yet. Does anyone have an idea how?

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58

1 Answers1

1

From this very similar question, one of the answers, by kjhughes, offers these solutions :

XPath 2.0 Solutions

  1. Use lower-case():
    /html/body//text()[contains(lower-case(.),'test')]
  2. Use matches() regex matching with its case-insensitive flag:
    /html/body//text()[matches(.,'test', 'i')]

So taking the first approach (i.e. lower-case()) with your examples:

//a[contains(lower-case(@title), "(' + lower-case(args.Code) + ')")]

//a[lower-case(@title)=\'' + lower-case(args.Code) + '\']

Update

From this answer:

upper-case() and lower-case() are XPath 2.0 functions. Chances are your platform supports XPath 1.0 only1

Try the first solution (as suggested in the answer by Tomalak):

//a[contains(translate(@title, \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\'), "(translate(' + args.Code + ', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')")]

//a[translate(@title, \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')=translate(\'' + args.Code + '\', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\') ]

Unfortunately, this requires knowledge of the alphabet the text uses. For plain English, the above probably works, but if you expect accented characters, make sure you add them to the list.1


1https://stackoverflow.com/a/1625859/1575353

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • While your xpath seems like it actualy could work Selenium respond to it with the fellowing error. -Failed to load Selenium IDE extensions: SyntaxError: expected expression, got keyword 'case'- Also the word case in notepad++ is highlighted in blue for some reasons. – vincent lavoie Jun 26 '17 at 15:44
  • yeah it doesn't appear that you included the entire string literals in your example but apparently you are using a single -quote (i.e. `'`) to delimit the strings. I update the examples to escape the single-quotes in the `translate()` calls... presumably you already figured that out – Sᴀᴍ Onᴇᴌᴀ Jun 26 '17 at 16:56
  • the first line you gave is suposed to translate a title such as catlitter (litter) and only find the '(litter)' therefore the ( ) is necesary. – vincent lavoie Jun 27 '17 at 18:32