1

Is it possible to create an action chain in selenium if one of the required elements will not exist until the action chain begins executing?

For example, I am trying to perform a drag and drop in selenium. The catch is that the target of the drop only exists while a drag is occurring. The intuitive solution for a drag and drop seems to be doing:

source = driver.find_element(...)
target = driver.find_element(...)
ActionChains(driver).drag_and_drop(source, target)

This will not work because target does not exist until a drag has begun.

I have tried doing something along the lines of:

source = driver.find_element(...)
drag_and_drop = ActionChains(driver)
drag_and_drop.click_and_hold(source)
drag_and_drop.move_by_offset(10, 10)
drag_and_drop.perform()

target = driver.find_element(...)
drag_and_drop.move_to_element(target)
drag_and_drop.release()
drag_and_drop.perform()

This also does not work. It seems like the mouse is released after the first perform which would cause the target to disappear. This seems to indicate that a single action chain with a single perform() call would be needed. I have tried researching to see if there is any way to lazily find an element when its step of the action chain is needed, but I could not find any way to do that.

Is there any way to achieve something like this in selenium?

Brett Tuttle
  • 140
  • 12

1 Answers1

1

This is actually a case of misdiagnosing the problem. The root issue actually lies in selenium not currently supporting native HTML5 drag and drop, which is better explained in this answer. So whether or not the required element exists, neither of the examples given would work for that purpose, even if they seem like they should.

With the issue of the element not existing at the start of the action chain the solution is actually to break it up into multiple sections with multiple perform() calls as shown in the second example. Again, the conclusion drawn in the question that multiple perform() calls does not work was inaccurate and a result of misunderstanding the true problem.

Community
  • 1
  • 1
Brett Tuttle
  • 140
  • 12