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.
https://www.helloweba.com/demo/2017/unlock/
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 elements = driver.find_element_by_id ("xx")# Target position to which the anchor element is to be moved 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.
---------------Gorgeous split-------------------------------------
Let's see how I did it.
fromSeleniumImportWebdriver fromSelenium.webdriver.common.action_chainsImportActionchains fromSelenium.common.exceptionsImportunexpectedalertpresentexception fromTimeImportSleepdriver=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 ()#left mouse button pressed not put forIndexinchRange (200): Try: Action.move_by_offset (2, 0). Perform ()#move the mouse in parallel exceptunexpectedalertpresentexception: Breakaction.reset_actions () Sleep (0.1)#wait for pause time#Print Warning box promptSuccess_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!
"Turn" Selenium simulate jquery slide unlock