Tornado
Tornado

Reputation: 127

getting attribute via xpath query succesfull in browser, but not in Robot Framework

I have a certain XPATH-query which I use to get the height from a certain HTML-element which returns me perfectly the desired value when I execute it in Chrome via the XPath Helper-plugin.

//*/div[@class="BarChart"]/*[name()="svg"]/*[name()="svg"]/*[name()="g"]/*[name()="rect" and @class="bar bar1"]/@height

However, when I use the same query via the Get Element Attribute-keyword in the Robot Framework

Get Element Attribute//*/div[@class="BarChart"]/*[name()="svg"]/*[name()="svg"]/*[name()="g"]/*[name()="rect" and @class="bar bar1"]/@height

... then I got an InvalidSelectorException about this XPATH.

InvalidSelectorException: Message: u'invalid selector: Unable to locate an 
element with the xpath expression `//*/div[@class="BarChart"]/*[name()="svg"]/*
[name()="svg"]/*[name()="g"]/*[name()="rect" and @class="bar bar1"]/`

So, the Robot Framework or Selenium removed the @-sign and everything after it. I thought it was an escape -problem and added and removed some slashes before the @height, but unsuccessful. I also tried to encapsulate the result of this query in the string()-command but this was also unsuccessful.

Does somebody has an idea to prevent my XPATH-query from getting broken?

Upvotes: 0

Views: 5416

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386240

You are correct in your observation that the keyword seems to removes everything after the final @. More correctly, it uses the @ to separate the element locator from the attribute name, and does this by splitting the string at that final @ character.

No amount of escaping will solve the problem as the code isn't doing any parsing at this point. This is the exact code (as of this writing...) that performs that operation:

def _parse_attribute_locator(self, attribute_locator):
    parts = attribute_locator.rpartition('@')
    ...

The simple solution is to drop that trailing slash, so your xpath will look like this:

//*/div[@class="BarChart"]/... and @class="bar bar1"]@height`

Upvotes: 0

JLRishe
JLRishe

Reputation: 101738

It looks like you can't include the attribute axis in the XPath itself when you're using Robot. You need to retrieve the element by XPath, and then specify the attribute name outside that. It seems like the syntax is something like this:

 Get Element Attribute xpath=(//*/div[@class="BarChart"]/*[name()="svg"]/*[name()="svg"]/*[name()="g"]/*[name()="rect" and @class="bar bar1"])@height 

or perhaps (I've never used Robot):

 Get Element Attribute xpath=(//*/div[@class="BarChart"]/*[name()="svg"]/*[name()="svg"]/*[name()="g"]/*[name()="rect" and @class="bar bar1"])[1]@height 

This documentation says

attribute_locator consists of element locator followed by an @ sign and attribute name, for example "element_id@class".

so I think what I've posted above is on the right track.

Upvotes: 2

Related Questions