This article mainly introduces Python selenium simulation jquery sliding unlock example, with a certain reference value, interested in small partners can refer to
This article describes the Python selenium simulation jquery sliding unlock instance, share to everyone, and leave a note for yourself
Sliding unlock has been one of the difficulties of UI Automation, I made a slide to unlock the example, I hope to do the Web UI Automation testing students some ideas.
First, let's look at an example.
When I manually click the slider, the style is changed:
1, Slide-to-unlock-handle represents the slider, the left margin of the slider is getting bigger (because it moves to the right!). )
2, Slide-tounlock-progress said after the slide after the background yellow, yellow width is increasing, because the sliding through the place is yellowing.
Besides, nothing else has changed, so we use the mouse to drag seemingly not! Because dragging a mouse is moving one element to another element. Such
# position element's original position, element = driver.find_element_by_id ("xx") # position element to be moved to target destination = driver.find_element_by_id ("xx") Actionchains (Driver). Drag_and_drop (element, target). Perform ()
But the position of the element has not changed during my manual presentation.
Let's see how I did it.
From selenium import webdriverfrom selenium.webdriver.common.action_chains import Actionchainsfrom Selenium.common.exceptions Import unexpectedalertpresentexceptionfrom Time Import sleepdriver = Webdriver. Chrome () driver.get ("https://www.helloweba.com/demo/2017/unlock/") Dragger = Driver.find_elements_by_class_name (" Slide-to-unlock-handle ") [0]action = Actionchains (Driver) action.click_and_hold (Dragger). Perform () #鼠标左键按下不放for Index in range: try: action.move_by_offset (2, 0). Perform () #平行移动鼠标 except Unexpectedalertpresentexception: break action.reset_actions () Sleep (0.1) #等待停顿时间 # Print Warning box prompt Success_ Text = Driver.switch_to.alert.textprint (success_text) sleep (5) Driver.quit ()
Driver.find_elements_by_class_name ("Slide-to-unlock-handle") [0]
First, I have several sliders on the page I want to manipulate, and I'll first find all the first of them through the class property.
Click_and_hold ()
Press the left mouse button on the slider with the Click_and_hold () method.
Move_by_offset ()
Next is the position of the slider through the For loop, the first parameter of the Move_by_offset () method is the x-axis, the second parameter is the y-axis, in pixels. Because it is a parallel move, Y is set to 0. X move 22 pixels at a time.
When the unlock succeeds, it throws the unexpectedalertpresentexception exception and jumps out of the loop after snapping.
Each cycle sleep 0.1 seconds, the smaller the time interval, the more smooth moving!
After the core steps have been introduced, the next step is to get the alert box above the message and print it, then close the browser.
Printing results are:
Successfully unlock!