3

I am trying to drag and drop using Python and Selenium.

I have two containers with rows which indicate file names and by drag and drop, I want to transfer one file from the first container to the second one.

This is how containers look: Containers

I have seen on the internet that selenium and drag and drop libraries had some issues and I am not really sure how to make this work.

As the version of browsers and drivers play an important role, please have a look at the ones I am using: Drivers / Browsers versions

The way am trying to drag and drop is the following:

time.sleep(15)
source_element = self.browser.find_element_by_css_selector('#transfer-tape-listing-nst0 > tbody > tr.file.transfer-tape-item.ui-draggable.ui-draggable-handle > td.listing-name')
dest_element = self.browser.find_element_by_css_selector('#transfer-location-listing > tbody > tr:nth-child(1) > td.listing-name.ui-droppable > span.listing-name-label')
ActionChains(self.browser).drag_and_drop(source_element, dest_element).perform()

My driver has this configuration:

def set_up_browser():
    display = Display(visible=0, size=(1200, 800))
    display.start()
    selected_browser = config_section_map('Test')['browser']
    browser = ''

    if selected_browser == 'Firefox':
        opts = FirefoxOptions()
        opts.set_headless(headless=False)
        firefox_profile = webdriver.FirefoxProfile()
        firefox_profile.set_preference("browser.privatebrowsing.autostart", False)
        firefox_profile.set_preference("marionette", True) # remove if causing issues

        browser = webdriver.Firefox(executable_path="/usr/bin/drivers/geckodriver", firefox_profile=firefox_profile, firefox_options=opts)
    elif selected_browser == 'Chrome':
        opts = ChromeOptions()
        opts.set_headless(headless=False)
        opts.add_argument("--no-sandbox")
        opts.add_argument("--incognito")
        browser = webdriver.Chrome(executable_path="/usr/bin/drivers/chromedriver", chrome_options=opts)
    elif selected_browser == 'edge':
         pass
    else:
        print 'unsupported browser'
        exit

    browser.set_window_size(1600, 1000)
    # browser.maximize_window()
    browser.delete_all_cookies()
    browser.implicitly_wait(int(config_section_map('Test')['wait']))
    browser.refresh()

    return browser

Can someone help me please with resolving this using Python? I have tried with both drivers and both fail. I don't get any exception, but the action doesn't work.

What happens is that the driver clicks on the source element and hovers on the target.

This is how containers look after executing the script: After execution

Thanks.

  • Does it work when you run your browser in not-headless mode? – Alexey R. Sep 19 '18 at 10:51
  • @AlexeyR. i have just tried it. When i execute i get WebDriverException: Message: unknown error: failed to change window state to maximized, current state is normal when the script tries to execute browser.maximize_window(). If i comment the line for maximizing my script fails in general because the screen is to small and many elements are hidden or on top of other elements. – Marialena Sep 19 '18 at 10:57
  • You mean your browsers fail to maximize in a regular (NON-headless) mode? – Alexey R. Sep 19 '18 at 11:01
  • @Marialena try with opts.add_argument("--start-maximized") – NarendraR Sep 19 '18 at 11:07
  • 1
    Instead of using drag_and_drop, can you try creating the action chain for the operation. Like moveTo(firstElement), clickAndHold, moveTo(secondElement), release and see whether it makes a difference? – Dakshinamurthy Karra Sep 20 '18 at 12:05
2

The problem was because I was using the drag_and_drop command.

Drag and drop now work with both Chrome and Firefox with the following command:

    source_element = self.browser.find_element_by_xpath("//*[@id='transfer-tape-listing-nst" + str(drive) + "']/tbody/tr[2]")
    dest_element = self.browser.find_element_by_xpath("//*[@id='transfer-location-listing']/tbody/tr[2]/td[1]")
    time.sleep(5)

    try:
        ActionChains(self.browser).click_and_hold(source_element).move_to_element(dest_element).release(dest_element).perform()
        # ActionChains(self.browser).drag_and_drop(source_element, dest_element)
        alert = self.browser.switch_to_alert() # xpath throws an exception some times
        alert.accept()
    except:
        pass

The browser setup and the version of the drivers are the ones I have in my question. Note that I have also swapped CSS with XPath in case this affected the selection of the elements.

Hope this helps! And thanks everyone for trying to help!

  • I am sorry for being late here, I am in the same situation now. But the solution proposed is not working for me :( – Hansiemithun May 22 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.