Assign extracted element value to a variable

2,364 views
Skip to first unread message

finspin

unread,
Sep 19, 2012, 7:16:36 AM9/19/12
to robotframe...@googlegroups.com
I'd like to store the element value which I extracted with xpath to a variable but I can't seem to make it work.

Here is the HTML:
<option value="test1">John Smith</option>

Here is what I tried but doesn't work:
${var1} =    xpath=//option[@value="test1"]/text()
${var1} =    Get Element Attribute    xpath=//option[@value="test1"]/text()

I need the element value to be used in the Select From List keyword.

Any ideas?

Kevin O.

unread,
Sep 19, 2012, 5:17:15 PM9/19/12
to robotframe...@googlegroups.com
I think you're looking for Get Text, which is not in any released version of S2L.
You can either manually patch S2L by copying get_text() from  https://github.com/rtomac/robotframework-selenium2library/blob/master/src/Selenium2Library/keywords/_element.py
Or
you can create a user library that works with Selenium2Library as is. Calling a private method like this is bad form, but this is just a workaround for a missing keyword.

from robot.libraries.BuiltIn import BuiltIn 

class MyS2LExtensions(object):
    def get_text(self, locator):
        """Returns the text value of element identified by `locator`.

        See `introduction` for details about locating elements.
        """
        s2l = BuiltIn().get_library_instance('Selenium2Library')
        return s2l._get_text(locator)

Kevin

Tatu aalto

unread,
Sep 19, 2012, 6:02:03 PM9/19/12
to korm...@gmail.com, robotframe...@googlegroups.com

Ugh

Without knowing anything about S2L, would it be possible to have same sort of keyword as in SL: Call selenium api?

Somehow I am under the impression that it could solve some these missing keyword problems.

-Tatu

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/robotframework-users/-/SkiBllvHS2oJ.
To post to this group, send email to robotframe...@googlegroups.com.
To unsubscribe from this group, send email to robotframework-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/robotframework-users?hl=en.
Message has been deleted

Kevin O.

unread,
Sep 21, 2012, 12:39:01 AM9/21/12
to robotframe...@googlegroups.com, korm...@gmail.com
Not only possible but easy to do in a user keyword. Some things are accomplished by fetching attributes of a WebDriver instance, but S2L monkey patches WebDriver so that it has methods instead.

| *** Settings *** |
| Library        | Selenium2Library |

| *** Test Cases *** |
| How NOT To Use Selenium2Library |
|    | Open Browser | http://jenkins-ci.org/ | ie |
|    | ${element} | Call Selenium API | find_element_by_id | page |
|    | Log | ${element.text} |

| *** Keywords *** |
| Call Selenium API |
|    | [Arguments] | ${method} | @{varargs} |
|    | ${S2L} | Get Library Instance | Selenium2Library |
|    | ${WebDriver} | Call Method | ${S2L} | _current_browser |
|    | ${return value} | Call Method | ${WebDriver} | ${method} | @{varargs} |
|    | [Return] | ${return value} |
Reply all
Reply to author
Forward
0 new messages