Reputation: 55
I would like to check in my test (selenium / python) creating a new image on the site. I need to load a picture for it. How can I do this when I have the option to use the drop or file explorer field. As far as I know, I can not use the file explorer because there is no id or name. How to use the drag and drop method?
I have this code but it does not work:
element = driver.find_elements_by_id ("file_upload")
element.send_keys ( "C: \\ Users \\ Desktop \\ Robert \\ Construct_ImminentCollision-C.jpg")
target = driver.find_element_by_id ("add-new-pano")
action_chains = ActionChains (driver)
action_chains.drag_and_drop (element, target) .perform ()
"add-new-pano" is the drop field id.
Upvotes: 2
Views: 6822
Reputation: 25
While I haven't used selenium recently, if you're working in a static workspace where you can control most everything I would recommend using PyAutoGUI. It isn't a dynamic solution, but it does contain a left mouse emulation feature for drag and drop.
In the provided example, the mouse moves from the 100,100 location towards the bottom of the screen by 100 pixels:
>>> pyautogui.moveTo(100,100) #starting mouse location of 100,100
>>> pyautogui.dragTo(100,200, button='left') # drag mouse to X of 100, Y of 200 while holding down left mouse button
You can setup a makeshift solution to your application if it can be done in a controlled environment every time where you know the resolution, location of the file explorer, etc.
I have run into issues in a windows env. before due to unexpected update screens. Food for thought...
Upvotes: 2
Reputation: 7873
The drag'n'drop of selenium is last time I checked broken.
I use the solution provided in How to simulate HTML5 Drag and Drop in Selenium Webdriver?
Upvotes: 1