Skip to content

Commit 0c25caa

Browse files
committed
Merge pull request #356 from emanlove/webdriver-element
Allow keywords to use Web Elements in addition to Locator.
2 parents 7664b8d + 6262611 commit 0c25caa

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

src/Selenium2Library/__init__.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ class Selenium2Library(
3333
imported into your Robot test suite (see `importing` section), and the
3434
`Open Browser` keyword must be used to open a browser to the desired location.
3535
36-
= Locating elements =
36+
**--- Note important change starting with Version 1.7.0 release ---**
37+
= Locating or specifying elements =
3738
3839
All keywords in Selenium2Library that need to find an element on the page
39-
take an argument, `locator`. By default, when a locator value is provided,
40-
it is matched against the key attributes of the particular element type.
41-
For example, `id` and `name` are key attributes to all elements, and
42-
locating elements is easy using just the `id` as a `locator`. For example::
40+
take an argument, either a `locator` or now a `webelement`. `locator`
41+
is a string that describes how to locate an element using a syntax
42+
specifying different location strategies. `webelement` is a variable that
43+
holds a WebElement instance, which is a representation of the element.
44+
45+
Using 'locator'
46+
---------------
47+
By default, when a locator value is provided, it is matched against the
48+
key attributes of the particular element type. For example, `id` and
49+
`name` are key attributes to all elements, and locating elements is easy
50+
using just the `id` as a `locator`. For example::
4351
4452
Click Element my_element
4553
@@ -68,6 +76,17 @@ class Selenium2Library(
6876
This can be fixed by changing the locator to:
6977
| Click Link default=page?a=b
7078
79+
Using 'webelement'
80+
------------------
81+
Starting with version 1.7 of the Selenium2Library, one can pass an argument
82+
that contains a WebElement instead of a string locator. To get a WebElement,
83+
use the new `Get WebElements` keyword. For example:
84+
85+
| ${elem} = | Get WebElements | id=my_element |
86+
| Click Element | ${elem} | |
87+
88+
Locating Tables, Table Rows, Columns, etc.
89+
------------------------------------------
7190
Table related keywords, such as `Table Should Contain`, work differently.
7291
By default, when a table locator value is provided, it will search for
7392
a table with the specified `id` attribute. For example:

src/Selenium2Library/keywords/_element.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
from selenium.webdriver.common.keys import Keys
22
from selenium.webdriver.common.action_chains import ActionChains
3+
from selenium.webdriver.remote.webelement import WebElement
34
from Selenium2Library import utils
45
from Selenium2Library.locators import ElementFinder
56
from Selenium2Library.locators import CustomLocator
67
from keywordgroup import KeywordGroup
78

9+
try:
10+
basestring # attempt to evaluate basestring
11+
def isstr(s):
12+
return isinstance(s, basestring)
13+
except NameError:
14+
def isstr(s):
15+
return isinstance(s, str)
16+
817
class _ElementKeywords(KeywordGroup):
918

1019
def __init__(self):
1120
self._element_finder = ElementFinder()
1221

22+
# Public, get element(s)
23+
24+
def get_webelements(self, locator):
25+
"""Returns list of WebElement objects matching locator.
26+
27+
See `introduction` for details about locating elements.
28+
"""
29+
return self._element_find(locator, False, True)
30+
1331
# Public, element lookups
1432

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

639657
def _element_find(self, locator, first_only, required, tag=None):
640658
browser = self._current_browser()
641-
elements = self._element_finder.find(browser, locator, tag)
642-
if required and len(elements) == 0:
643-
raise ValueError("Element locator '" + locator + "' did not match any elements.")
644-
if first_only:
645-
if len(elements) == 0: return None
646-
return elements[0]
659+
if isstr(locator):
660+
elements = self._element_finder.find(browser, locator, tag)
661+
if required and len(elements) == 0:
662+
raise ValueError("Element locator '" + locator + "' did not match any elements.")
663+
if first_only:
664+
if len(elements) == 0: return None
665+
return elements[0]
666+
elif isinstance(locator, WebElement):
667+
elements = locator
668+
# do some other stuff here like deal with list of webelements
669+
# ... or raise locator/element specific error if required
647670
return elements
648671

649672
def _frame_contains(self, locator, text):

test/acceptance/keywords/elements.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
*** Settings ***
2-
Suite Setup Go To Page "links.html"
2+
Test Setup Go To Page "links.html"
33
Resource ../resource.txt
44

55
*** Test Cases ***
6+
Get Elements
7+
@{links}= Get WebElements //div[@id="div_id"]/a
8+
Length Should Be ${links} 11
9+
10+
More Get Elements
11+
[Setup] Go To Page "forms/prefilled_email_form.html"
12+
@{checkboxes}= Get WebElements //input[@type="checkbox"]
13+
Length Should Be ${checkboxes} 2
14+
:For ${checkbox} in @{checkboxes}
15+
\ Unselect Checkbox ${checkbox}
16+
:For ${checkbox} in @{checkboxes}
17+
\ Checkbox Should Not Be Selected ${checkbox}
18+
:For ${checkbox} in @{checkboxes}
19+
\ Select Checkbox ${checkbox}
20+
:For ${checkbox} in @{checkboxes}
21+
\ Checkbox Should Be Selected ${checkbox}
22+
623
Assign Id To Element
724
[Documentation] Tests also Reload Page keyword.
825
Page Should Not Contain Element my id

0 commit comments

Comments
 (0)