Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a3da65f
Added 'Get Element' and 'Get Elements' keywords.
emanlove Jul 27, 2014
d475be3
Allow keywords to use WebDriver Element in addition to Locator.
emanlove Jul 27, 2014
dabcabd
Fixed issues with code change to use elements in addtion to locators.
emanlove Jul 27, 2014
d980419
Added tests for new keyword 'Get Elements'
emanlove Jul 27, 2014
5d82653
Removed 'Get WebElement' (singular) keyword.
emanlove Aug 31, 2014
7ad9866
Renamed 'Get Elements' to 'Get WebElements'.
emanlove Sep 1, 2014
4198266
Initial documentation for using WebElements and Locators
emanlove Oct 7, 2014
da4259a
Updated documentation for locating objects using WebElements.
emanlove Oct 12, 2014
505e6b3
Updated tests reflecting change in keyword name to 'Get WebElements'.
emanlove Oct 12, 2014
c7f6208
Added 'Get Element' and 'Get Elements' keywords.
emanlove Jul 27, 2014
81f479b
Allow keywords to use WebDriver Element in addition to Locator.
emanlove Jul 27, 2014
01ecb47
Fixed issues with code change to use elements in addtion to locators.
emanlove Jul 27, 2014
9b3e2b7
Added tests for new keyword 'Get Elements'
emanlove Jul 27, 2014
f90516a
Removed 'Get WebElement' (singular) keyword.
emanlove Aug 31, 2014
cee2d83
Renamed 'Get Elements' to 'Get WebElements'.
emanlove Sep 1, 2014
da2544c
Initial documentation for using WebElements and Locators
emanlove Oct 7, 2014
48e0e2b
Updated documentation for locating objects using WebElements.
emanlove Oct 12, 2014
7ee9e4f
Updated tests reflecting change in keyword name to 'Get WebElements'.
emanlove Oct 12, 2014
0e5452d
Merge branch 'webdriver-element' of github.com:emanlove/robotframewor…
emanlove Feb 2, 2015
0a70f45
Removed documentation stub for stale elements.
emanlove Feb 2, 2015
a2522b0
Update documentation with minor corrections.
emanlove May 3, 2015
8cdbd45
Merge branch 'master' into webdriver-element
emanlove May 19, 2015
6262611
Minor documentation and formatting corrections.
emanlove May 19, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/Selenium2Library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ class Selenium2Library(
imported into your Robot test suite (see `importing` section), and the
`Open Browser` keyword must be used to open a browser to the desired location.

= Locating elements =
**--- Note important change starting with Version 1.7.0 release ---**
= Locating or specifying elements =

All keywords in Selenium2Library that need to find an element on the page
take an argument, `locator`. By default, when a locator value is provided,
it is matched against the key attributes of the particular element type.
For example, `id` and `name` are key attributes to all elements, and
locating elements is easy using just the `id` as a `locator`. For example::
take an argument, either a `locator` or now a `webelement`. `locator`
is a string that describes how to locate an element using a syntax
specifying different location strategies. `webelement` is a variable that
holds a WebElement instance, which is a representation of the element.

Using 'locator'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backticks

---------------
By default, when a locator value is provided, it is matched against the
key attributes of the particular element type. For example, `id` and
`name` are key attributes to all elements, and locating elements is easy
using just the `id` as a `locator`. For example::

Click Element my_element

Expand Down Expand Up @@ -68,6 +76,17 @@ class Selenium2Library(
This can be fixed by changing the locator to:
| Click Link default=page?a=b

Using 'webelement'
------------------
Starting with version 1.7 of the Selenium2Library, one can pass an argument
that contains a WebElement instead of a string locator. To get a WebElement,
use the new `Get WebElements` keyword. For example:

| ${elem} = | Get WebElements | id=my_element |
| Click Element | ${elem} | |

Locating Tables, Table Rows, Columns, etc.
------------------------------------------
Table related keywords, such as `Table Should Contain`, work differently.
By default, when a table locator value is provided, it will search for
a table with the specified `id` attribute. For example:
Expand Down
35 changes: 29 additions & 6 deletions src/Selenium2Library/keywords/_element.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from Selenium2Library import utils
from Selenium2Library.locators import ElementFinder
from Selenium2Library.locators import CustomLocator
from keywordgroup import KeywordGroup

try:
basestring # attempt to evaluate basestring
def isstr(s):
return isinstance(s, basestring)
except NameError:
def isstr(s):
return isinstance(s, str)

class _ElementKeywords(KeywordGroup):

def __init__(self):
self._element_finder = ElementFinder()

# Public, get element(s)

def get_webelements(self, locator):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of Get WebElements, how would you feel about just Get Elements . Also, would it be useful to have a Get Element that only gets a single element, or would that be unnecessary/confusing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ombre42 and I discussed this over on #315. 'Get Elements' was my original name but changed it because it conflicted with the XML Library's keyword of the same name. So we settled on Get WebElements. I also had a 'Get WebElement' (singular) keyword in there but removed it for some reason. I was doing some recent testing with this and saw a possible need for getting a single web element.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Find Element and Find Elements then? It's not necessarily as clear, but that matches up with the selenium API. (I'm not really necessarily opposed to Get WebElements though).

"""Returns list of WebElement objects matching locator.

See `introduction` for details about locating elements.
"""
return self._element_find(locator, False, True)

# Public, element lookups

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think get_webelement would be useful here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

def current_frame_contains(self, text, loglevel='INFO'):
Expand Down Expand Up @@ -638,12 +656,17 @@ def remove_location_strategy(self, strategy_name):

def _element_find(self, locator, first_only, required, tag=None):
browser = self._current_browser()
elements = self._element_finder.find(browser, locator, tag)
if required and len(elements) == 0:
raise ValueError("Element locator '" + locator + "' did not match any elements.")
if first_only:
if len(elements) == 0: return None
return elements[0]
if isstr(locator):
elements = self._element_finder.find(browser, locator, tag)
if required and len(elements) == 0:
raise ValueError("Element locator '" + locator + "' did not match any elements.")
if first_only:
if len(elements) == 0: return None
return elements[0]
elif isinstance(locator, WebElement):
elements = locator
# do some other stuff here like deal with list of webelements
# ... or raise locator/element specific error if required

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything specific that goes here, or is this just a future note?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a future note...

return elements

def _frame_contains(self, locator, text):
Expand Down
19 changes: 18 additions & 1 deletion test/acceptance/keywords/elements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
*** Settings ***
Suite Setup Go To Page "links.html"
Test Setup Go To Page "links.html"
Resource ../resource.txt

*** Test Cases ***
Get Elements
@{links}= Get WebElements //div[@id="div_id"]/a
Length Should Be ${links} 11

More Get Elements
[Setup] Go To Page "forms/prefilled_email_form.html"
@{checkboxes}= Get WebElements //input[@type="checkbox"]
Length Should Be ${checkboxes} 2
:For ${checkbox} in @{checkboxes}
\ Unselect Checkbox ${checkbox}
:For ${checkbox} in @{checkboxes}
\ Checkbox Should Not Be Selected ${checkbox}
:For ${checkbox} in @{checkboxes}
\ Select Checkbox ${checkbox}
:For ${checkbox} in @{checkboxes}
\ Checkbox Should Be Selected ${checkbox}

Assign Id To Element
[Documentation] Tests also Reload Page keyword.
Page Should Not Contain Element my id
Expand Down