selenium3 + python - action_chains source code analysis

ActionChains Profile

actionchains selenium is devoted to the operation inside the mouse-related such as: a mouse movement, mouse button operation keys and context menu (right mouse) interaction.
This is useful for doing more complex actions, such as hovering and drag and drop.

actionchains and shortcuts may be used in combination, such as ctrl, shif, alt used in conjunction with the mouse

When you use actionchains object methods, behavioral event in actionchains target queue storage. When you use the perform (), event execution order.

  • Method One: You can write a long list
    • menu = driver.find_element_by_css_selector(".nav")
    • hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
    • ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
  • Method Two: You can write several steps

    menu = driver.find_element_by_css_selector(".nav")

    hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

    actions = ActionChains(driver)

    actions.move_to_element(menu)

    actions.click(hidden_submenu) actions.perform()

 

Methods Introduction 

Keys class 1 mentioned below, there is a keyboard event classes selenium, introduction method:

from selenium.webdriver.common.keys import Keys

2. Use the mouse events import method:

from selenium.webdriver.common.action_chains import ActionChains

ActionChains class (Object): 
DEF __init __ (Self, Driver):
self._driver = Driver
self._actions = []

DEF the perform (Self):

# execute behavior Event

def click (self, on_element = None ):
Click on:
- If the parameter do not write, then click on the current mouse position
- If the adjustment to the positioning element object element, that is the point of this element

def click_and_hold (self, on_element = None ):
left mouse button and hold an element
- if the argument is not to write, so the point is that the current mouse position
- If the adjustment navigate to the element object element, that is the point of this element

def context_click (self, on_element = None ):
right click
- if the argument is not written, then the point is that the current mouse position
- If the adjustment to the positioning element object element, that is the point of this element

def double_click (self, on_element = None ):
Double-click the mouse
- If the parameters are not written, then the point is that the current mouse position
- If the adjustment to the positioning element object element, that is the point of this element

def drag_and_drop (self, source, target ):
the left mouse button while holding down the source element, then move to the target element and release the mouse button
- source: Hold down the mouse elements position
- target: release the mouse element position

def drag_and_drop_by_offset (self, source, xoffset , yoffset):
the left mouse button while holding down the source element, then move the target offset and release the mouse button.
- source: element holding down the mouse position
- xoffset: X
offset axis
- yoffset: Y
offset axis

def key_down (self, value, element = None):
only send a key without releasing it. It applies only modifier keys (control, alt and shift).

- value: modifier keys to be sent. Values are defined in "Keys" category.
- element: positioned elements
if the element parameters do not write is the current position of the mouse

for example, hold down the
C :: + Ctrl

. ActionChains (Driver) .key_down (Keys.CONTROL) .send_keys ( 'C') KEY_UP (Keys.CONTROL) .perform ()

DEF KEY_UP (Self, value, Element = None):

# key is released, used together with the above

def move_by_offset (self, xoffset, yoffset ):
move the mouse to the current mouse position offset

- xoffset: x-axis
as a positive or negative integer moved to offset x
- yoffset: Y-axis
partial shift, as a positive or negative integer.

def move_to_element (self, to_element):
Hover
- to_element: positioning requires hover elements

def move_to_element_with_offset (self, to_element, xoffset , yoffset):
by moving the mouse specified element offset. Upper left corner of the element relative offset
- to_element: positioning requires hover elements
- xoffset: X
-offset
- yoffset: Y
axis offset

def release (self, on_element = None ):
releasing the mouse button on an element.

- If the parameter is not written, it is the current mouse position
- If the adjustment to the positioning element object element, and that is the element.

DEF send_keys (Self, * keys_to_send):
Send focus to the current element
key to send. Modifier key constant can be "Keys" class.

def send_keys_to_element (self, element, * keys_to_send):
transmits to the positioning element
- element: positioning element
- keys_to_send: key to be transmitted. Modifier key constant can be "Keys" class.

1. To achieve the function key combination of Ctrl + F5

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keys import Keys

import time driver = webdriver.Chrome()

driver.get("https://www.baidu.com")

the time.sleep ( 3) # achieve Ctrl + F5 to refresh

ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.F5).key_up(Keys.CONTROL).perform()

 

 

Reprinted: https://www.cnblogs.com/yoyoketang/p/8711367.html

 

Guess you like

Origin www.cnblogs.com/Teachertao/p/11025568.html