def mouseClick(css:str):
global driver
actions = ActionChains(driver)
el = waitElem(css)
actions.move_to_element(el).click().perform()
python类ActionChains()的实例源码
def click(self, *modifiers):
"""
Clicks the element, optionally while pressing the given modifier keys.
Note that support for holding a modifier key is currently experimental, and may not work
at all.
:param modifiers: modifier keys to press while clicking
:Example: Click an element
browser.element(name='new_user_button').click()
:Example: Click an element with shift key pressed
from selenium.webdriver.common.keys import Keys
browser.element(name='new_user_button').click(Keys.SHIFT)
:Example: Click an element with several modifier keys pressed
from selenium.webdriver.common.keys import Keys
browser.element(name='new_user_button').click(Keys.SHIFT, Keys.CONTROL)
"""
def method():
if modifiers:
action = ActionChains(self.driver)
for mod in modifiers:
action.key_down(mod)
action.click(self.el)
for mod in modifiers:
action.key_up(mod)
action.perform()
else:
self.el.click()
self._element_call(method, self.wait_for_enabled)
self.browser.after_hooks.run()
def double_click(self):
"""
Double clicks the element.
Note that browser support may vary.
:Example: Double-click an element
browser.element(name='new_user_button').double_click()
"""
self._element_call(lambda: ActionChains(self.driver).double_click(self.el)
.perform(), self.wait_for_present)
self.browser.after_hooks.run()
def right_click(self):
"""
Right clicks the element
Note that browser support may vary
:Example: Right click an element
browser.element(name='new_user_button').right_click()
"""
self._element_call(lambda: ActionChains(self.driver).context_click(self.el)
.perform(), self.wait_for_present)
self.browser.after_hooks.run()
def hover(self):
"""
Moves the mouse to the middle of this element
Note that browser support may vary
:Example: Hover over an element
browser.element(name='new_user_button').hover()
"""
self._element_call(lambda: ActionChains(self.driver).move_to_element(self.el)
.perform(), self.wait_for_present)
self.browser.after_hooks.run()
def hover(self, element):
hover = ActionChains(self.browser).move_to_element(element)
hover.perform()
def _reconcile_drag_preshot(self):
ofxdiv = self.browser.find_element_by_id('ofx-2-0')
logger.info('ofxdiv location: %s size: %s',
ofxdiv.location, ofxdiv.size)
pos_x = (ofxdiv.location['x'] - 400) + (ofxdiv.size['width'] / 4)
pos_y = (ofxdiv.location['y'] - 50) + (ofxdiv.size['height'] / 2)
self.browser.execute_script(
"$('body').append($('%s'));" % self._cursor_script(pos_x, pos_y)
)
actions = ActionChains(self.browser)
actions.move_to_element(ofxdiv)
actions.click_and_hold()
actions.move_by_offset(-400, -50)
actions.perform()
self.browser.get_screenshot_as_file('docs/source/foo.png')
def __init__(self, url, webdriver, **kwargs):
kwargs.setdefault('body', webdriver.page_source)
kwargs.setdefault('encoding', 'utf-8')
super(WebdriverResponse, self).__init__(url, **kwargs)
self.actions = ActionChains(webdriver)
self.webdriver = webdriver
def mouse_over(self, locator):
"""Moves the mouse pointer over the element found by the `locator`.
:param locator: An instance of :class:`XPath` or a string containing an XPath expression.
"""
xpath = six.text_type(locator)
el = self.find_element(xpath)
actions = ActionChains(self.web_driver)
actions.move_to_element(el)
actions.perform()
def open_urls(self, urls, tab_limit=MAX_TABS):
"""
This works at least on Chrome across platforms.
"""
# First setup a document with the target URLs.
link_doc = 'data:text/html,'
link_doc += '<html><head><title>Links!</title></head><body>'
id = 0
for url in urls:
link_doc += '<a id="%d" href="%s">%d: %s</a><br>' % (
id, url, id, url)
id += 1
link_doc += '</body><html>'
self.driver.get(link_doc)
# Now open each document in a new tab by ctrl+shift clicking the
# anchor.
for tag in self.driver.find_elements_by_tag_name("a"):
action = ActionChains(self.driver)
if mozinfo.os == "mac":
ctrl_key = Keys.COMMAND
else:
ctrl_key = Keys.CONTROL
action.key_down(ctrl_key).key_down(Keys.SHIFT).click(
tag).key_up(Keys.SHIFT).key_up(ctrl_key).perform()
time.sleep(self.per_tab_pause)
time.sleep(self.settle_wait_time)
self.stats.print_stats()
def drag_drop_test(driver,
x_offset=0,
y_offset=0,
element_class="gt_slider_knob"):
"""????
:param x_offset:????x????
:param y_offset: ????y????
:param element_class: ???????css??
:return:
"""
dragger = driver.find_element_by_class_name(element_class)
action = ActionChains(driver)
action.drag_and_drop_by_offset(dragger, x_offset, y_offset).perform()
time.sleep(2.8)
def click_element_at_coordinates(self, locator, xoffset, yoffset):
"""Click element identified by `locator` at x/y coordinates of the element.
Cursor is moved and the center of the element and x/y coordinates are
calculted from that point.
Key attributes for arbitrary elements are `id` and `name`. See
`introduction` for details about locating elements.
"""
self._info("Click clicking element '%s' in coordinates '%s', '%s'." % (locator, xoffset, yoffset))
element = self._element_find(locator, True, True)
#self._element_find(locator, True, True).click()
#ActionChains(self._current_browser()).move_to_element_with_offset(element, xoffset, yoffset).click().perform()
ActionChains(self._current_browser()).move_to_element(element).move_by_offset(xoffset, yoffset).click().perform()
def double_click_element(self, locator):
"""Double click element identified by `locator`.
Key attributes for arbitrary elements are `id` and `name`. See
`introduction` for details about locating elements.
"""
self._info("Double clicking element '%s'." % locator)
element = self._element_find(locator, True, True)
ActionChains(self._current_browser()).double_click(element).perform()
def drag_and_drop(self, source, target):
"""Drags element identified with `source` which is a locator.
Element can be moved on top of another element with `target`
argument.
`target` is a locator of the element where the dragged object is
dropped.
Examples:
| Drag And Drop | elem1 | elem2 | # Move elem1 over elem2. |
"""
src_elem = self._element_find(source,True,True)
trg_elem = self._element_find(target,True,True)
ActionChains(self._current_browser()).drag_and_drop(src_elem, trg_elem).perform()
def drag_and_drop_by_offset(self, source, xoffset, yoffset):
"""Drags element identified with `source` which is a locator.
Element will be moved by xoffset and yoffset, each of which is a
negative or positive number specify the offset.
Examples:
| Drag And Drop By Offset | myElem | 50 | -35 | # Move myElem 50px right and 35px down. |
"""
src_elem = self._element_find(source, True, True)
ActionChains(self._current_browser()).drag_and_drop_by_offset(src_elem, xoffset, yoffset).perform()
def mouse_over(self, locator):
"""Simulates hovering mouse over the element specified by `locator`.
Key attributes for arbitrary elements are `id` and `name`. See
`introduction` for details about locating elements.
"""
self._info("Simulating Mouse Over on element '%s'" % locator)
element = self._element_find(locator, True, False)
if element is None:
raise AssertionError("ERROR: Element %s not found." % (locator))
ActionChains(self._current_browser()).move_to_element(element).perform()
def mouse_up(self, locator):
"""Simulates releasing the left mouse button on the element specified by `locator`.
Key attributes for arbitrary elements are `id` and `name`. See
`introduction` for details about locating elements.
"""
self._info("Simulating Mouse Up on element '%s'" % locator)
element = self._element_find(locator, True, False)
if element is None:
raise AssertionError("ERROR: Element %s not found." % (locator))
ActionChains(self._current_browser()).release(element).perform()
def open_context_menu(self, locator):
"""Opens context menu on element identified by `locator`."""
element = self._element_find(locator, True, True)
ActionChains(self._current_browser()).context_click(element).perform()
def mouse_down_on_link(self, locator):
"""Simulates a mouse down event on a link.
Key attributes for links are `id`, `name`, `href` and link text. See
`introduction` for details about locating elements.
"""
element = self._element_find(locator, True, True, 'link')
ActionChains(self._current_browser()).click_and_hold(element).perform()
def mouse_down_on_image(self, locator):
"""Simulates a mouse down event on an image.
Key attributes for images are `id`, `src` and `alt`. See
`introduction` for details about locating elements.
"""
element = self._element_find(locator, True, True, 'image')
ActionChains(self._current_browser()).click_and_hold(element).perform()