Example analysis of how to use selenium to simulate jquery sliding unlock in Python

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.