Ignore case Xpath @Name attribute c# Selenium/Appium

411 Views Asked by At

I have a C# selenium/Appium project where I need to find a desktop Application window By.Xpath("").

This works:

By.XPath("//*[@Name='ASDASD']");

However, some builds of the app have the window name be "ASDasd", which causes the Xpath above to not find the window element and the test fails.

Is it possible to Ignore the case of the @Name attribute whether it be "ASDASD", "ASDasd" or something else?

I did try using the XPath translate function, but I am not able to find the element, I assume I am doing it wrong.

What I tried:

By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")

or

By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd']")

or

By.XPath("//*[@Name='translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd'']")

or

By.XPath("//*[@Name='translate(asdasd,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')']")

Maybe some other variations too, but I could not get it to work.

Some of the examples may have invalid formatting.

While other seems to be valid but could not find the element and it would timeout.

UPDATE: Thank you for the assistance, this worked:

By.XPath("//*[translate(@Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='asdasd']");

However, it added 60 seconds to the test somehow, it seems to stall for 60 seconds on one of the places where it looks for the main window.

Thanks for the help!

Regards

1

There are 1 best solutions below

0
On BEST ANSWER

name() gives you the name of context node. In this case (//*), the name of whatever element you are currently looking at. You meant to write @Name, i.e. the attribute that happens to be called Name.

By.XPath("//*[translate(@Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")

Using translate() is clunky and fails when the search string contains unanticipated characters.

Unfortunately, there is no lower-case() function in XPath 1.0. But you can work around this limitation with the help of the host language (such as C#).

The following will dynamically create an XPath expression which finds arbitrary values case-insensitively:

var searchValue = "asdasd";
var uc = searchValue.ToUpperInvariant();
var lc = searchValue.ToLowerInvariant();

var xpath = $"//*[translate(@Name, '{uc}', '{lc}') = '{lc}']";
// -> "//*[translate(@Name, 'ASDASD', 'asdasd') = 'asdasd']"