To achieve the action of dragging the seal , I introduce ActionChains Class simulation mouse drag , But it didn't work
# Drag by specifying coordinates , No effect drag_and_drop_by_offset().perform()
# adopt source and target Specify two positions to drag , No effect drag_and_drop().perform() # Then I tried to drag step by step , It still doesn't work
click_and_hold().perform() move_to_element().perform() release().perform()
Elements can be located , But I tried Selenium ActionChains All drag methods , There is no drag effect , Search information online , There are several answers ：
* Selenium Of Action There is no support HTML5 The problem of drag and drop , Can pass js Write a drag method , Then call .
* Physical mode simulates mouse operation .
* introduce pyautogui package , realization web Front end with track drag function , solve ActionChains Drag failure problem .
<> Physical mode simulates mouse operation
import win32api, win32con # Import these two packages # Move to a point coordinate win32api.SetCursorPos((782, 355))
time.sleep(0.5) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 782, 355)
time.sleep(0.5) win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 300, 0) time.
sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
# This physical model has no effect , And I think it's weird , There is no positioning element , It's just a physical simulation , It's a bit of a departure from automated testing , And it's unstable
<> use pyautogui package , Drag and drop
The one with the seal div There's one in the box draggable=“true”, It's draggable , however Selenium Bring your own ActionChains All the drag methods have no effect , The built-in method is to start from the point in an instant A It's time B, It is also possible that new elements are generated when the seal is dragged , The drag method has no effect , There's some interaction in the front , Must have the mouse track to be able to successfully realize the drag and drop function .
import pyautogui # Locate starting element source = driver.find_element_by_xpath('') #
Move the mouse over the start element pyautogui.moveTo(source .location['x']+20, source .location['y']+125
) # Locate the location element to drag to target= driver.find_element_by_xpath('') # Realize drag and drop function pyautogui.
dragTo(target.location['x']+20, target.location['y']+155, duration=1)
It's been a day , Search a lot of methods to know that the introduction of this package will be so simple to solve the problem of drag , I've been puzzling about why the drag method has no effect /(ㄒoㄒ)/~~
UI The road of automation , Some pits are known only when they are stepped on , The company's automated testing is just starting , The current testing work can cope with it , But I don't want to just do some functional business testing , Make time for yourself to study , There's no big guy to fly with , You can only search your own information when you encounter problems that you will not , Maybe it only takes a minute to solve the problem , It's going to take a day to figure out what's wrong , Then find a solution . However, no matter for work or career development , It's all good . come on. ! Carry the banner of automation !O(∩_∩)O