XML name with '*' causing problems with XPath

Viewed 58

I am trying to write XPath for use in Selenium tests in Robot framework.

The problem I am running into is that an identifier I want to use is called id*, and the * in there is causing problems.

The XPath I want to use is //input[@type='text' and @id*='F_Date']

When I try to run this test I get an error saying it is not a valid XPath expression.

Removing the * makes it valid XPath, but does not find the element. Unfortunately there is not a different identifier I can replace id* with.

Does anyone know how to work around this?

2 Answers

A * character is not allowed in XML attribute (or element) names:

Attribute     ::= Name Eq AttValue
Name          ::= NameStartChar (NameChar)*
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] |
                  [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] |
                  [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
                  [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
                  [#x10000-#xEFFFF]
NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] |
                  [#x203F-#x2040]

Source: W3C XML BNF for component names

There is no "way around this." Choose another character for the XML attribute name. If you receive a document that uses id* as an attribute value name, the document is not XML; tell the owner to fix it or provide you with tools/libraries to process it because you cannot use XML tools/libraries because it's not XML.

NOTE:- '*' character is not allowed in XML attribute or element Names.

Please try below code:

.//input[@type='text' and contains(@id,'F_Date')]
Related