Selenium2Library Get Element Attribute can't get attribute value

4,152 views
Skip to first unread message

Michael Tingey

unread,
Nov 25, 2014, 12:52:11 PM11/25/14
to robotframe...@googlegroups.com
I'm trying to get the value of an attribute ('div' element, 'rowid' attribute) using "Get Element Attribute" but am getting the below error.  I'm trying to get the value for this entry, rowid="3f0df873-ff38-4637-a69e-831040b9d0ce".

I've verified my xpath using XPath Checker in Firefox and also in Firebug.  I've also tried different xpath forms which also verify using XPath Checker and Firebug but to no avail.

In looking at the Selenium2Libary documentation for "Get Element Attribute" is seems that its looking for a specific form ("element_id@class") that I can't seem to achieve due to having to go back up the tree for the div element.  

I'm new to XPath so perhaps there is a way to get this in the form that the S2L keyword is expecting or maybe there is a better locator strategy.

Any insights into "Get Element Attribute" or perhaps a better approach would be greatly appreciated.


My XPath:  
//*[text()='Advanced Operator Tasks']/ancestor::div[1]/@rowid

My HTML:
<div parentid="" rowindex="12" rowid="3f0df873-ff38-4637-a69e-831040b9d0ce" visualindex="12" role="row" class="gridxRow gridxRowSelected" data-rowheight="21" aria-selected="true">
    <table cellspacing="0" cellpadding="0" border="0" role="presentation" class="gridxRowTable">
        <tbody>
        <tr>
            <td style="width:250px;min-width:250px;max-width:250px;" class="gridxCell" colid="name" aria-describedby="rolesTable_grid-name" tabindex="-1" role="gridcell" aria-readonly="true">Advanced 
            Operator Tasks</td>
            <td style="width:431px;min-width:431px;max-width:431px;" class="gridxCell" colid="description" aria-describedby="rolesTable_grid-description" tabindex="-1" role="gridcell" aria-readonly="true">Advanced 
            Operator Tasks</td>
        </tr>
        </tbody>
    </table>
</div>

ERROR:
------------------------------------------------------------------------------
Create User Template :: describe test here                            | FAIL |
InvalidSelectorException: Message: The given selector //*[text()='Advanced Opera
tor Tasks']/ancestor::div[1]/@rowid is either invalid or does not result in a We
bElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression "//*[text()='Advanced O
perator Tasks']/ancestor::div[1]/@rowid" is: [object Attr]. It should be an elem
ent.
Stacktrace:
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///c:/users/ibm_ad~1/ap
pdata/local/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriver@googleco
de.com/components/driver-component.js:9615:1)
    at FirefoxDriver.prototype.findElementsInternal_ (file:///c:/users/ibm_ad~1/
appdata/local/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriver@google
code.com/components/driver-component.js:9673:9)
    at FirefoxDriver.prototype.findElements (file:///c:/users/ibm_ad~1/appdata/l
ocal/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdr...@googlecode.com/
components/driver-component.js:9677:3)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ibm_ad~1/ap
pdata/local/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriver@googleco
de.com/components/command-processor.js:11635:16)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ibm_ad~1/appd
ata/local/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriver@googlecode
.com/components/command-processor.js:11640:7)
    at DelayedCommand.prototype.execute/< (file:///c:/users/ibm_ad~1/appdata/loc
al/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdr...@googlecode.com/co
mponents/command-processor.js:11582:5)
------------------------------------------------------------------------------
HMC User Mgmt User Template :: User Management Scenario 01: Verifi... | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
==============================================================================

From the Selenium2Library docs:  
http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Get%20Element%20Attribute

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


Tatu Aalto

unread,
Nov 26, 2014, 1:24:21 AM11/26/14
to michae...@gmail.com, robotframework-users

Ugh

Your selector is not correct, to be precise it is not valid xpath. In you case xpath to select correct element would be:

//*[text()='Advanced Operator Tasks']/ancestor::div[1]

Then to get the row id attribute with Get Element Attribute [1] keyword, you must define the argument in this way:
xpath=//*[text()='Advanced Operator Tasks']/ancestor::div[1]@rowid

-Tatu
Send from my mobile
[1] http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Get%20Element%20Attribute

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Michael Tingey

unread,
Nov 26, 2014, 8:39:36 AM11/26/14
to robotframe...@googlegroups.com
Hi Tatu,

Thank you for the reply.  

I've tried many different xpath forms and had actually guessed at this form but both XPath Checker and Firebug indicated a syntax error so never tried it.
xpath=//*[text()='Advanced Operator Tasks']/ancestor::div[1]@rowid

Seeing your reply I thought maybe the checkers are wrong so I reran with your suggestion but am getting the below error.

When I use "XPath Checker" with this form it returns '3f0df873-ff38-4637-a69e-831040b9d0ce'.
xpath=//*[text()='Advanced Operator Tasks']/ancestor::div[1]/@rowid

Any other suggestions are appreciated.


------------------------------------------------------------------------------
Create User Template :: describe test here                            | FAIL |
InvalidSelectorException: Message: The given selector //*[text()='Advanced Opera
tor Tasks']/ancestor::div[1]@rowid is either invalid or does not result in a Web
Element. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //*[
text()='Advanced Operator Tasks']/ancestor::div[1]@rowid because of the followin
g error:
SyntaxError: The expression is not a legal expression.
Stacktrace:
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///c:/users/ibm_ad~1/ap
pdata/local/temp/tmp4yghz8/webdriver-py-profilecopy/extensions/fxdriver@googleco
de.com/components/driver-component.js:9615:1)
    at FirefoxDriver.prototype.findElementsInternal_ (file:///c:/users/ibm_ad~1/
appdata/local/temp/tmp4yghz8/webdriver-py-profilecopy/extensions/fxdriver@google
code.com/components/driver-component.js:9673:9)
    at FirefoxDriver.prototype.findElements (file:///c:/users/ibm_ad~1/appdata/l
ocal/temp/tmp4yghz8/webdriver-py-profilecopy/extensions/fxdr...@googlecode.com/
components/driver-component.js:9677:3)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ibm_ad~1/ap
pdata/local/temp/tmp4yghz8/webdriver-py-profilecopy/extensions/fxdriver@googleco
de.com/components/command-processor.js:11635:16)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ibm_ad~1/appd
ata/local/temp/tmp4yghz8/webdriver-py-profilecopy/extensions/fxdriver@googlecode
.com/components/command-processor.js:11640:7)
    at DelayedCommand.prototype.execute/< (file:///c:/users/ibm_ad~1/appdata/loc
al/temp/tmp4yghz8/webdriver-py-profilecopy/extensions/fxdr...@googlecode.com/co
mponents/command-processor.js:11582:5)
------------------------------------------------------------------------------
HMC User Mgmt User Template :: HMC User Mgmt User Template: Verifi... | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
==============================================================================
appdata/l
ocal/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriv...@googlecode.com/
components/driver-component.js:9677:3)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ibm_ad~1/ap
pdata/local/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriver@googleco
de.com/components/command-processor.js:11635:16)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ibm_ad~1/appd
ata/local/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriver@googlecode
.com/components/command-processor.js:11640:7)
    at DelayedCommand.prototype.execute/< (file:///c:/users/ibm_ad~1/
appdata/loc
al/temp/tmpfqexp1/webdriver-py-profilecopy/extensions/fxdriv...@googlecode.com/co
mponents/command-processor.js:11582:5)
------------------------------------------------------------------------------
HMC User Mgmt User Template :: User Management Scenario 01: Verifi... | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
==============================================================================

From the Selenium2Library docs:  

Markus Bernhardt

unread,
Nov 26, 2014, 8:51:39 AM11/26/14
to michae...@gmail.com, robotframe...@googlegroups.com
Hi Michael,

could you please first try, whether this delivers the rowid:
xpath=//div[@rowindex=12]@rowid

Then perhaps something like:
xpath=//td[text()='Advanced Operator Tasks']/../..@rowid

Cheers,
Markus

Tatu Aalto

unread,
Nov 26, 2014, 2:41:32 PM11/26/14
to markus.b...@me.com, michae...@gmail.com, robotframe...@googlegroups.com
Ugh

I did had an idea on the way back on home, but it was too long to write it with a mobile device and needed to be tested. Here are examples of successful case and failure case. I bet you are doing it like in the failure example. The only difference between two test cases is which way the selector is used in the two S2L library keywords.

Then some tips.
1) Next time, run the test with trace level [1] enabled, it gives stack trace and one can see clearly on which keyword the error did happen and lot more.

2) Because you said that you are new to xpath, reading some tutorial would be useful. I did start with reading w3schools [2] tutorial and did help me a lot to understand the power of xpath. But one has to remember that xpath is lot more than is covered in the w3schools tutorial, so reading the actual standard [3] from W3C is most likely the next stop after one has the basics covered.

*** Settings ***
Library           Selenium2Library

*** Test Cases ***
successful example
    Selenium2Library.Open Browser    https://www.google.com
    Selenium2Library.Wait Until Page Contains Element
    ...    xpath=//input[@id="gbqfq"]/ancestor::div[@id="gs_lc0"]
    ...    timeout=5
    ${attrib}=    Selenium2Library.Get Element Attribute    xpath=//input[@id="gbqfq"]/ancestor::div[@id="gs_lc0"]@style
    Should Be Equal As Strings
    ...    ${attrib}
    ...    position: relative;
    [Teardown]    Selenium2Library.Close All Browsers

failure example
    Selenium2Library.Open Browser    https://www.google.com
    Selenium2Library.Wait Until Page Contains Element
    ...   
xpath=//input[@id="gbqfq"]/ancestor::div[@id="gs_lc0"]@style
    ...    timeout=5
    ${attrib}=    Selenium2Library.Get Element Attribute   
xpath=//input[@id="gbqfq"]/ancestor::div[@id="gs_lc0"]@style
    Should Be Equal As Strings
    ...    ${attrib}
    ...    position: relative;
    [Teardown]    Selenium2Library.Close All Browsers
 

Michael Tingey

unread,
Nov 26, 2014, 8:15:50 PM11/26/14
to robotframe...@googlegroups.com, michae...@gmail.com
Hi Markus,

Based on your's and Tatu's replies I realized that I was expecting 'Get Element Attribute' to use an XPath compliant form.

It finally dawned on me that the general form for this keyword <element_id>@<attribute> is not intended to be a legal XPath form (except when broken into the two parts).  XPath Checker is great but misapplied by me in this case.

I have gotten this to return the value of the attribute correctly!

Thank you for all help!
Markus
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Michael Tingey

unread,
Nov 26, 2014, 8:16:54 PM11/26/14
to robotframe...@googlegroups.com, markus.b...@me.com, michae...@gmail.com
Hi Tatu,

The two examples you created were great and helped me to figure out that I was completely missing the 'Get Element Attribute' locator form.  It seems pretty obvious now.

Thank you for the detailed response and tips.  Also, the tutorials you pointed me to.  I've been using w3schools xpath tutorial and slowly absorbing the W3C XPath reference.

Thanks for helping out a newbie!
Markus
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages