3

I want to locate an element with attribute $9a but apparently the dollar sign is causing problems. When I use expression:

//td[@id='isc_6T']//span[@$9a='browse'] 

I get the following exception

The given selector //td[@id='isc_6T']//span[@$9a='browse'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //td[@id='isc_6T']//span[@$9a='browse'] because of the following error:
SyntaxError: The expression is not a legal expression.
Command duration or timeout: 9 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55'
System info: host: 'daniel', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-39-generic', java.version: '1.7.0_65'
Session ID: 7fdd584b-10b8-4c5a-ab64-72fe7cff7e2a
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=33.0}]

EDIT: This is the HTML part that I am trying to locate. (id is dynamic so it can't be used directly)

<span handlenativeevents="false" $9a="browse" id="isc_T9" role="button">
<img border="0" align="TEXTTOP" height="18" width="18" draggable="true" suppress="TRUE" style="vertical-align:middle;margin-top:0px;margin-bottom:0px;" id="isc_TA" src="some_image.png">
</span>

I have already created a workaround with parsing HTML with Jsoup to determine current dynamic id but I still want a cleaner solution

Danio
  • 1,064
  • 1
  • 11
  • 25

3 Answers3

3

The '$' sign cannot appear in an XML attribute name, and the XPath syntax for names reflects the XML syntax.

According to this answer:

What characters are allowed in an HTML attribute name?

Dollar signs and many other things are allowed in HTML5 attribute names. However, unless someone has defined a mapping from such names to valid XML names, they can't be referenced from XPath; and I'm not aware of any such mapping.

Community
  • 1
  • 1
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

If you don't have to use XPath and want to use css it's fairly easy. Just a simple \ will handle the $

[\$9a='browse'] 
Saifur
  • 16,081
  • 6
  • 49
  • 73
0

The following might work:

//td[@id='isc_6T']//span[attribute::*[name() = '$9a' and . = 'browse']]

Michael Kay already mentioned that this is not allowed in XML, so this might not work with all XPath engines. However at least with IntelliJ's "search by xpath" it works.

yankee
  • 38,872
  • 15
  • 103
  • 162