I. Basic Grammar
- Generate an action actions=ActionChains(driver)
- Action adding method actions. Method
- Execute actions.perform()
II. Method list
Click (on element = none) - left click Click and hold (on element = none) context_click(on_element=None) - right click Double click (on element = none) - double click the left mouse button Drag and drop (source, target) - drag to an element and release Drag and drop by offset (source, xoffset, yoffset) Key down (value, element = none) Key up (value, element = none) -- release a key Move by offset (xoffset, yoffset) - move the mouse from the current position to a certain coordinate Move? To? Element (to? Element) -- move the mouse to an element Move ﹐ to ﹐ element ﹐ with ﹐ offset (to ﹐ element, xoffset, yoffset) -- the position to move to the distance from an element (upper left coordinate) perform() -- perform all actions in the chain Release (on element = none) - release the left mouse button at an element position send_keys(*keys_to_send) -- send a key to the current focus element Send key to element (element, * keys to send)
III. drag implementation (slider verification will use move)
Two ways of implementation
#I'll skip the boring code in front #Method 1: actions=ActionChains(driver) #Get the action chain object actions.drag_and_drop(sourse,target).perform() #Where sourse is the start element object and target is the end position element object #Generally used to drag an image from one place to another #Mode two: actions=ActionChains(driver) #Get the action chain object actions.click_and_hold(Moving objects).perform() actions.move_by_offset(x,y).perform() #x is the moving horizontal distance, y is the moving vertical distance, positive and negative represents left and right or up and down actions.release().perform() #Let it go. Don't forget this step
IV. human like sliding block
Give me a segment code ha
def get_stacks(distance): #distance is the total length of sliding distance += 20 ''' //Uniform acceleration / deceleration operation v = v0 + a * t //Displacement: s = v * t + 0.5 * a * (t**2) ''' # Initial velocity v0 = 0 # List of acceleration and deceleration a_list = [20,40, 5] # time t = 0.1 # initial position s = 0 # Slide track forward forward_stacks = [] mid = distance * 3 / 5 while s < distance: if s < mid: a = a_list[random.randint(0, 1)] else: a = -a_list[2] v = v0 stack = v * t + 0.5 * a * (t ** 2) # Displacement obtained each time stack = round(stack) s += stack v0 = v + a * t forward_stacks.append(stack) back_stacks = [-1, -1, -2, -3, -2, -3, -2, -2, -3, -1] return {'forward_stacks': forward_stacks, 'back_stacks': back_stacks} stacks = get_stacks(60) print(stacks) forward_stacks = stacks['forward_stacks'] back_stacks = stacks['back_stacks'] slider_button = driver.find_element_by_class_name('The class name of sliding things') ActionChains(driver).click_and_hold(slider_button).perform() for forward_stack in forward_stacks: ActionChains(driver).move_by_offset(xoffset=forward_stack, yoffset=0).perform() for back_stack in back_stacks: ActionChains(driver).move_by_offset(xoffset=back_stack, yoffset=0).perform() time.sleep(0.3) ActionChains(driver).move_by_offset(xoffset=5, yoffset=0).perform() time.sleep(0.2) ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform() time.sleep(0.1) ActionChains(driver).move_by_offset(xoffset=1, yoffset=0).perform() time.sleep(0.1) ActionChains(driver).move_by_offset(xoffset=-1, yoffset=0).perform() ActionChains(driver).release().perform()