2

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.

2 Answers2

2

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...

bigdz23
  • 25
  • 4
1

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?

amirouche
  • 7,682
  • 6
  • 40
  • 94
  • 1
    I don't think this is what the OP intended. The OP mentioned that he wants to upload a picture to a website using the **drag and drop** method *programmatically*. Not by moving a puzzle-like element from a place to another. – Joe Aug 30 '19 at 15:44