diff --git a/README.md b/README.md index d116c72..a4322c0 100755 --- a/README.md +++ b/README.md @@ -407,6 +407,7 @@ This means you can pass selectors instead of locators to all Se2Lib methods that - for maintainability and readability, you should pass selectors to Se2Lib methods, not locators. - if you write your own helper methods for finding or interacting with elements allow them to be passed locators *and* selectors. +- You can also pass an instance of a selenium WebElement to Se2Lib methods instead of a selector or locator #### Looking up elements from the end of a list @@ -494,7 +495,8 @@ we don't actually need a reference to the WebElement because all page objects gi so from within your page object you can call them on `self`. If, for some reason, you need a direct reference to a WebElement you can get it by passing a locator/selector to -`find_element` or `find_element`, which is also on every page object. When at all possible, however, +`find_element` or `find_element`, which is also on every page object. You can then use this reference when +invoking Se2Lib keywords instead of a locator/selector. When at all possible, however, work at the Selenium2Library level, not at the WebElement level. For example: ... diff --git a/robotpageobjects/base.py b/robotpageobjects/base.py index 509d2d5..4ec82fa 100755 --- a/robotpageobjects/base.py +++ b/robotpageobjects/base.py @@ -9,6 +9,7 @@ from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import WebDriverException +from selenium.webdriver.remote.webelement import WebElement from Selenium2Library import Selenium2Library from Selenium2Library.keywords.keywordgroup import KeywordGroupMetaClass from . import abstractedlogger @@ -920,11 +921,18 @@ def _element_find(self, locator, *args, **kwargs): Try to use _element_find with the locator as is, then if a selector exists, try that. - :param locator: The Selenium2Library-style locator, or IFT selector. - :type locator: str + + ``locator`` can also be a WebElement if an element has been identified already + and it is desired to perform actions on that element + + :param locator: The Selenium2Library-style locator, or IFT selector + or WebElement (if the element has already been identified). + :type locator: str or WebElement :returns: WebElement or list """ + if isinstance(locator, WebElement): + return locator our_wait = self.selenium_implicit_wait if kwargs.get("wait") is None else kwargs["wait"] diff --git a/tests/scenarios/test_find_elements_with_selector.py b/tests/scenarios/test_find_elements_with_selector.py index f6658a0..3ca8d0e 100644 --- a/tests/scenarios/test_find_elements_with_selector.py +++ b/tests/scenarios/test_find_elements_with_selector.py @@ -25,6 +25,11 @@ def test_find_element(self): def test_find_element_multiple(self): self.page.find_element("inputs") + def test_find_element_webelement(self): + element_found_by_selector = self.page.find_element("search-button") + self.page.click_element(element_found_by_selector) + self.page.location_should_be("/site/result.html?q=search%20term") + def tearDown(self): self.page.close()