本文整理汇总了Python中selenium.webdriver.remote.webelement.WebElement方法的典型用法代码示例。如果您正苦于以下问题:Python webelement.WebElement方法的具体用法?Python webelement.WebElement怎么用?Python webelement.WebElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.remote.webelement
的用法示例。
在下文中一共展示了webelement.WebElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _expected_condition_find_element
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def _expected_condition_find_element(self, element):
"""Tries to find the element, but does not thrown an exception if the element is not found
:param element: PageElement or element locator as a tuple (locator_type, locator_value) to be found
:returns: the web element if it has been found or False
:rtype: selenium.webdriver.remote.webelement.WebElement or appium.webdriver.webelement.WebElement
"""
from toolium.pageelements.page_element import PageElement
web_element = False
try:
if isinstance(element, PageElement):
# Use _find_web_element() instead of web_element to avoid logging error message
element._web_element = None
element._find_web_element()
web_element = element._web_element
elif isinstance(element, tuple):
web_element = self.driver_wrapper.driver.find_element(*element)
except NoSuchElementException:
pass
return web_element
示例2: _expected_condition_find_element_stopped
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def _expected_condition_find_element_stopped(self, element_times):
"""Tries to find the element and checks that it has stopped moving, but does not thrown an exception if the element
is not found
:param element_times: Tuple with 2 items where:
[0] element: PageElement or element locator as a tuple (locator_type, locator_value) to be found
[1] times: number of iterations checking the element's location that must be the same for all of them
in order to considering the element has stopped
:returns: the web element if it is clickable or False
:rtype: selenium.webdriver.remote.webelement.WebElement or appium.webdriver.webelement.WebElement
"""
element, times = element_times
web_element = self._expected_condition_find_element(element)
try:
locations_list = [tuple(web_element.location.values()) for i in range(int(times)) if not time.sleep(0.001)]
return web_element if set(locations_list) == set(locations_list[-1:]) else False
except StaleElementReferenceException:
return False
示例3: _expected_condition_find_element_containing_text
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def _expected_condition_find_element_containing_text(self, element_text_pair):
"""Tries to find the element and checks that it contains the specified text, but does not thrown an exception if the element is
not found
:param element_text_pair: Tuple with 2 items where:
[0] element: PageElement or element locator as a tuple (locator_type, locator_value) to be found
[1] text: text to be contained into the element
:returns: the web element if it contains the text or False
:rtype: selenium.webdriver.remote.webelement.WebElement or appium.webdriver.webelement.WebElement
"""
element, text = element_text_pair
web_element = self._expected_condition_find_element(element)
try:
return web_element if web_element and text in web_element.text else False
except StaleElementReferenceException:
return False
示例4: _expected_condition_value_in_element_attribute
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def _expected_condition_value_in_element_attribute(self, element_attribute_value):
"""Tries to find the element and checks that it contains the requested attribute with the expected value,
but does not thrown an exception if the element is not found
:param element_attribute_value: Tuple with 3 items where:
[0] element: PageElement or element locator as a tuple (locator_type, locator_value) to be found
[1] attribute: element's attribute where to check its value
[2] value: expected value for the element's attribute
:returns: the web element if it contains the expected value for the requested attribute or False
:rtype: selenium.webdriver.remote.webelement.WebElement or appium.webdriver.webelement.WebElement
"""
element, attribute, value = element_attribute_value
web_element = self._expected_condition_find_element(element)
try:
return web_element if web_element and web_element.get_attribute(attribute) == value else False
except StaleElementReferenceException:
return False
示例5: swipe
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def swipe(self, element, x, y, duration=None):
"""Swipe over an element
:param element: either a WebElement, PageElement or element locator as a tuple (locator_type, locator_value)
:param x: horizontal movement
:param y: vertical movement
:param duration: time to take the swipe, in ms
"""
if not self.driver_wrapper.is_mobile_test():
raise Exception('Swipe method is not implemented in Selenium')
# Get center coordinates of element
center = self.get_center(element)
initial_context = self.driver_wrapper.driver.current_context
if self.driver_wrapper.is_web_test() or initial_context != 'NATIVE_APP':
center = self.get_native_coords(center)
# Android needs absolute end coordinates and ios needs movement
end_x = x if self.driver_wrapper.is_ios_test() else center['x'] + x
end_y = y if self.driver_wrapper.is_ios_test() else center['y'] + y
self.driver_wrapper.driver.swipe(center['x'], center['y'], end_x, end_y, duration)
if self.driver_wrapper.is_web_test() or initial_context != 'NATIVE_APP':
self.driver_wrapper.driver.switch_to.context(initial_context)
示例6: driver_wrapper
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def driver_wrapper():
# Create a mock element
global mock_element
mock_element = mock.MagicMock(spec=WebElement)
mock_element.find_element.return_value = child_element
mock_element.text = 'text value'
mock_element.get_attribute.return_value = 'input text value'
# Reset wrappers pool values
DriverWrappersPool._empty_pool()
DriverWrapper.config_properties_filenames = None
# Create a new wrapper
driver_wrapper = DriverWrappersPool.get_default_wrapper()
driver_wrapper.driver = mock.MagicMock()
return driver_wrapper
示例7: driver_wrapper
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def driver_wrapper():
"""Create a new mock element and a new driver before each test"""
global mock_element
mock_element = mock.MagicMock(spec=WebElement)
mock_element.find_element.return_value = child_element
# Reset wrappers pool values
DriverWrappersPool._empty_pool()
DriverWrappersPool.output_directory = ''
DriverWrapper.config_properties_filenames = None
# Create a new wrapper
driver_wrapper = DriverWrappersPool.get_default_wrapper()
driver_wrapper.driver = mock.MagicMock()
return driver_wrapper
示例8: element_exists
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def element_exists(browser, locator, delay=CHECK_IF_EXISTS_TIMEOUT, locator_strategy = By.CSS_SELECTOR):
result = False
try:
element = find_element(browser, locator, locator_strategy, delay)
result = type(element) is WebElement
except NoSuchElementException:
log.debug('No such element. SELECTOR=' + locator)
# print the session_id and url in case the element is not found
# noinspection PyProtectedMember
log.debug("In case you want to reuse session, the session_id and _url for current browser session are: {},{}".format(browser.session_id, browser.command_executor._url))
except TimeoutException:
log.debug('No such element. SELECTOR=' + locator)
except Exception as element_exists_error:
log.error(element_exists_error)
log.debug("Check your locator: {}".format(locator))
# noinspection PyProtectedMember
log.debug("In case you want to reuse session, the session_id and _url for current browser session are: {},{}".format(browser.session_id, browser.command_executor._url))
finally:
log.debug("{} ({})".format(str(result), locator))
return result
示例9: set_select_by_text
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def set_select_by_text(self, select, text):
'''
Set the selected value of a select element by the visible text.
Parameters
----------
select: str or selenium.webdriver.remote.webelement.WebElement
Any valid CSS selector or a selenium element
text: str
The visible text in the select element option. (Not the value)
'''
if isinstance(select, str):
elem = self.get_element(select)
else:
elem = select
sel = Select(elem)
sel.select_by_visible_text(text)
示例10: set_select_by_value
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def set_select_by_value(self, select, value):
'''
Set the selected value of a select element by the value.
Parameters
----------
select: str or selenium.webdriver.remote.webelement.WebElement
Any valid CSS selector or a selenium element
value: str
The value on the select element option. (Not the visible text)
'''
if isinstance(select, str):
elem = self.get_element(select)
else:
elem = select
sel = Select(elem)
sel.select_by_value(value)
示例11: get_links
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def get_links(self, what='a'):
'''
Gets links by CSS selector or WebElement list.
Parameters
----------
what: str or list of WebElement
A CSS selector to search for. This can be any valid CSS selector.
-- or --
A list of previously selected WebElement instances.
Returns
-------
list of str
A list of URL strings.
'''
elems = self._selector_or_elements(what)
urls = []
for elem in elems:
href = elem.get_attribute('href')
if href:
urls.append(href)
return urls
示例12: get_elements
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def get_elements(self, selector):
'''
Gets elements by CSS selector.
Parameters
----------
selector: str
A CSS selector to search for. This can be any valid CSS selector.
Returns
-------
list of selenium.webdriver.remote.webelement.WebElement
A list of selenium element objects.
'''
elems = self.find_elements(selector)
# Extend all the elements
if elems:
_elems = [WebRunnerElement(elem._parent, elem._id, elem._w3c) for elem in elems]
return _elems
else:
raise NoSuchElementException
示例13: find_element
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def find_element(self, selector):
'''
Finds an element by CSS/XPATH selector.
Parameters
----------
selector: str
A CSS/XPATH selector to search for. This can be any valid CSS/XPATH selector.
Returns
-------
selenium.webdriver.remote.webelement.WebElement or None
Returns an element or nothing at all
'''
elem = None
try:
if selector.startswith('/'):
elem = self.browser.find_element_by_xpath(selector)
else:
elem = self.browser.find_element_by_css_selector(selector)
except NoSuchElementException:
pass
return elem
示例14: find_elements
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def find_elements(self, selector):
'''
Finds elements by CSS/XPATH selector.
Parameters
----------
selector: str
A CSS/XPATH selector to search for. This can be any valid CSS/XPATH selector.
Returns
-------
list of selenium.webdriver.remote.webelement.WebElement or list
Returns a list of elements or empty list
'''
elems = []
try:
if selector.startswith('/'):
elems = self.browser.find_elements_by_xpath(selector)
else:
elems = self.browser.find_elements_by_css_selector(selector)
except NoSuchElementException:
pass
return elems
示例15: _element_find
# 需要导入模块: from selenium.webdriver.remote import webelement [as 别名]
# 或者: from selenium.webdriver.remote.webelement import WebElement [as 别名]
def _element_find(self, locator, first_only, required, tag=None):
application = self._current_application()
elements = None
if isstr(locator):
_locator = locator
elements = self._element_finder.find(application, _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):
if first_only:
return locator
else:
elements = [locator]
# do some other stuff here like deal with list of webelements
# ... or raise locator/element specific error if required
return elements