Can't drag and drop from source to target using selenium and python



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



  • 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!



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2