|
1 | 1 | from selenium.webdriver.common.keys import Keys |
2 | 2 | from selenium.webdriver.common.action_chains import ActionChains |
| 3 | +from selenium.webdriver.remote.webelement import WebElement |
3 | 4 | from Selenium2Library import utils |
4 | 5 | from Selenium2Library.locators import ElementFinder |
5 | 6 | from Selenium2Library.locators import CustomLocator |
6 | 7 | from keywordgroup import KeywordGroup |
7 | 8 |
|
| 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 | + |
8 | 17 | class _ElementKeywords(KeywordGroup): |
9 | 18 |
|
10 | 19 | def __init__(self): |
11 | 20 | self._element_finder = ElementFinder() |
12 | 21 |
|
| 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 | + |
13 | 31 | # Public, element lookups |
14 | 32 |
|
15 | 33 | def current_frame_contains(self, text, loglevel='INFO'): |
@@ -638,12 +656,17 @@ def remove_location_strategy(self, strategy_name): |
638 | 656 |
|
639 | 657 | def _element_find(self, locator, first_only, required, tag=None): |
640 | 658 | 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 |
647 | 670 | return elements |
648 | 671 |
|
649 | 672 | def _frame_contains(self, locator, text): |
|
0 commit comments