无法使用Selenium和python从源拖放到目标

时间:2018-09-19 10:39:53

标签: python selenium testing selenium-webdriver drag-and-drop

我正在尝试使用Python和Selenium拖放。

我有两个带有行的容器,这些行指示文件名,并且通过拖放操作,我想将一个文件从第一个容器传输到第二个容器。

这是容器的外观: Containers

我在互联网上看到硒和拖放库存在一些问题,我不确定如何使这项工作有效。

由于浏览器和驱动程序的版本起着重要作用,请查看我使用的浏览器和驱动程序: Drivers / Browsers versions

我尝试拖放的方式如下:

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()

我的驱动程序具有以下配置:

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

container1的HTML代码:

<div class="table-responsive datatable-container transfer-container" tabindex="-1">



<!-- Loading Div -->
<div class="loading browse-files-loading" style="display: none;">
    <div class="cssload-squeeze-container">
        <div class="cssload-squeeze">
            <span></span><span></span><span></span><span></span><span></span>
        </div>
    </div>
</div>
<!-- End Loading Div -->
                                        <table class="table dark transfer-panel-table transfer-tape-listing" id="transfer-tape-listing-nst1" data-serial="" data-dev="nst1" data-tape="78" data-path="" data-isltfs="0" data-ltfs="" data-tapeset="test" data-show-hidden="0" data-empty="0">
                                            <thead>
                                            <tr>
                                                <th data-sort="string" width="55%">
                                                    Filename
                                                </th>
                                                <th data-sort="int" width="10%">
                                                    Size
                                                </th>
                                                <th data-sort="int" width="35%">
                                                    Modified
                                                </th>
                                            </tr>
                                            </thead>

                                            <tbody class="ui-droppable">
                                            <tr class="folder transfer-tape-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="Tom">  <td width="55%" class="listing-name ui-droppable" data-sort-value="name,Tom"><span class="fa fa-folder"></span><span class="listing-name-label">Tom</span></td>  <td width="15%" class="listing-size" data-sort-value="size,608">608.0 B</td>  <td width="35%" class="listing-modified" data-sort-value="modified,1537275412000">Yesterday at 1:56 PM</td></tr><tr style="cursor: pointer;" class="file transfer-tape-item ui-draggable ui-draggable-handle" data-id="35992" data-name="index.xml">  <td width="55%" class="listing-name" data-sort-value="name,index.xml"><span class="fa fa-file-o"></span><span class="listing-name-label">index.xml</span></td>  <td width="15%" class="listing-size" data-sort-value="size,1128">1.1 KB</td>  <td width="35%" class="listing-modified" data-sort-value="modified,1537353182000">Today at 11:33 AM</td></tr><tr style="cursor: pointer;" class="file transfer-tape-item ui-draggable ui-draggable-handle" data-id="35990" data-name="1.png">  <td width="55%" class="listing-name" data-sort-value="name,1.png"><span class="fa fa-file-image-o"></span><span class="listing-name-label">1.png</span></td>  <td width="15%" class="listing-size" data-sort-value="size,16865">16.5 KB</td>  <td width="35%" class="listing-modified" data-sort-value="modified,1537285426000">Yesterday at 4:43 PM</td></tr><tr data-name="" class="no-drag folder"><td colspan="3" style="height:100%;text-align:center;" class="ui-droppable">&nbsp;</td></tr></tbody>


                                        </table>
                                    </div>

container2的HTML代码:

<div class="table-responsive datatable-container transfer-container" tabindex="-1">



<!-- Loading Div -->
<div class="loading browse-files-loading" style="display: none;">
    <div class="cssload-squeeze-container">
        <div class="cssload-squeeze">
            <span></span><span></span><span></span><span></span><span></span>
        </div>
    </div>
</div>
<!-- End Loading Div -->
                                <table class="table dark transfer-panel-table" id="transfer-location-listing" data-show-hidden="0">
                                    <thead>
                                    <tr>
                                        <th data-sort="string" width="55%">
                                            Filename
                                        </th>
                                        <th data-sort="int" width="10%">
                                            Size
                                        </th>
                                        <th data-sort="int" width="35%">
                                            Modified
                                        </th>
                                    </tr>
                                    </thead>
                                    <tbody class="ui-droppable">

                                    <tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="Tom">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,Tom"><span class="fa fa-folder"></span><span class="listing-name-label">Tom</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537285422050">Yesterday at 4:43 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="Test">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,Test"><span class="fa fa-folder"></span><span class="listing-name-label">Test</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537353109338">Today at 11:31 AM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="__pycache__">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,__pycache__"><span class="fa fa-folder"></span><span class="listing-name-label">__pycache__</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537355943848">Today at 12:19 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name=".cache" data-hidden="">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,.cache"><span class="fa fa-folder"></span><span class="listing-name-label">.cache</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1523523101579">04/12/2018 at 9:51 AM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="ln_test">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,ln_test"><span class="fa fa-folder"></span><span class="listing-name-label">ln_test</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537346008828">Today at 9:33 AM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="test_symlinks">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,test_symlinks"><span class="fa fa-folder"></span><span class="listing-name-label">test_symlinks</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1523962020782">04/17/2018 at 11:47 AM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="test_folder1010">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,test_folder1010"><span class="fa fa-folder"></span><span class="listing-name-label">test_folder1010</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1523977431312">04/17/2018 at 4:03 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="Recycle Bin">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,Recycle Bin"><span class="fa fa-folder"></span><span class="listing-name-label">Recycle Bin</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1524651661126">04/25/2018 at 11:21 AM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="Recycle Bin2">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,Recycle Bin2"><span class="fa fa-folder"></span><span class="listing-name-label">Recycle Bin2</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1530003338614">06/26/2018 at 9:55 AM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="test">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,test"><span class="fa fa-folder"></span><span class="listing-name-label">test</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1532002786508">07/19/2018 at 1:19 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="test2">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,test2"><span class="fa fa-folder"></span><span class="listing-name-label">test2</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1532003585498">07/19/2018 at 1:33 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="web_drop">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,web_drop"><span class="fa fa-folder"></span><span class="listing-name-label">web_drop</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537285426920">Yesterday at 4:43 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="SpaceCopyTestFolder">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,SpaceCopyTestFolder"><span class="fa fa-folder"></span><span class="listing-name-label">SpaceCopyTestFolder</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537356003178">Today at 12:20 PM</td></tr><tr class="folder transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="SpaceCopyTestFolder2">  <td style="width:55%;" class="listing-name ui-droppable" data-sort-value="name,SpaceCopyTestFolder2"><span class="fa fa-folder"></span><span class="listing-name-label">SpaceCopyTestFolder2</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,">-</td><td style="width:35%;" class="listing-modified" data-sort-value="modified,1537356005668">Today at 12:20 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="generateRandomFiles.sh">  <td style="width:55%;" class="listing-name" data-sort-value="name,generateRandomFiles.sh"><span class="fa fa-file-o"></span><span class="listing-name-label">generateRandomFiles.sh</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,199">199 B</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1524490328274">04/23/2018 at 2:32 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name=".generateRandomFiles.sh.swp" data-hidden="">  <td style="width:55%;" class="listing-name" data-sort-value="name,.generateRandomFiles.sh.swp"><span class="fa fa-file-o"></span><span class="listing-name-label">.generateRandomFiles.sh.swp</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,0">0 B</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1523622380874">04/13/2018 at 1:26 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name=".generateRandomFiles.sh.swx" data-hidden="">  <td style="width:55%;" class="listing-name" data-sort-value="name,.generateRandomFiles.sh.swx"><span class="fa fa-file-o"></span><span class="listing-name-label">.generateRandomFiles.sh.swx</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,0">0 B</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1523622380874">04/13/2018 at 1:26 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="orgfile">  <td style="width:55%;" class="listing-name" data-sort-value="name,orgfile"><span class="fa fa-file-o"></span><span class="listing-name-label">orgfile</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,0">0 B</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1523891174277">04/16/2018 at 4:06 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="rbUnitTestsOut.xml">  <td style="width:55%;" class="listing-name" data-sort-value="name,rbUnitTestsOut.xml"><span class="fa fa-file-o"></span><span class="listing-name-label">rbUnitTestsOut.xml</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,4359">4 KB</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1524651675996">04/25/2018 at 11:21 AM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="generateBigFiles.sh">  <td style="width:55%;" class="listing-name" data-sort-value="name,generateBigFiles.sh"><span class="fa fa-file-o"></span><span class="listing-name-label">generateBigFiles.sh</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,200">200 B</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1524490313054">04/23/2018 at 2:31 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="LTO_Catalog_2018_09_07 11_44_48.json.gz">  <td style="width:55%;" class="listing-name" data-sort-value="name,LTO_Catalog_2018_09_07 11_44_48.json.gz"><span class="fa fa-file-archive-o"></span><span class="listing-name-label">LTO_Catalog_2018_09_07 11_44_48.json.gz</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,51108">50 KB</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1536317089066">09/07/2018 at 11:44 AM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="LTO_Catalog_2018_09_07 11_45_24.json.gz">  <td style="width:55%;" class="listing-name" data-sort-value="name,LTO_Catalog_2018_09_07 11_45_24.json.gz"><span class="fa fa-file-archive-o"></span><span class="listing-name-label">LTO_Catalog_2018_09_07 11_45_24.json.gz</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,51109">50 KB</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1536317125396">09/07/2018 at 11:45 AM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="LTO_Catalog_2018_09_07 11_47_00.json.gz">  <td style="width:55%;" class="listing-name" data-sort-value="name,LTO_Catalog_2018_09_07 11_47_00.json.gz"><span class="fa fa-file-archive-o"></span><span class="listing-name-label">LTO_Catalog_2018_09_07 11_47_00.json.gz</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,51113">50 KB</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1536317220476">09/07/2018 at 11:47 AM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="SpaceCopyTestFolder.txt">  <td style="width:55%;" class="listing-name" data-sort-value="name,SpaceCopyTestFolder.txt"><span class="fa fa-file-o"></span><span class="listing-name-label">SpaceCopyTestFolder.txt</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,380005">371 KB</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1536772782210">09/12/2018 at 6:19 PM</td></tr><tr class="file transfer-fs-item ui-draggable ui-draggable-handle" style="cursor: pointer;" data-name="1.png">  <td style="width:55%;" class="listing-name" data-sort-value="name,1.png"><span class="fa fa-file-image-o"></span><span class="listing-name-label">1.png</span></td>  <td style="width:10%;" class="listing-size" data-sort-value="size,16865">16 KB</td>  <td style="width:35%;" class="listing-modified" data-sort-value="modified,1537285426000">Yesterday at 4:43 PM</td></tr><tr data-name="" class="folder no-drag"><td colspan="4" style="height:100%;text-align:center;" class="ui-droppable">&nbsp;</td></tr></tbody>

                                </table>

                            </div>

有人可以帮助我使用Python解决此问题吗?我尝试过使用两个驱动程序,但都失败了。我没有任何例外,但该操作无效。

发生的情况是,驱动程序单击了源元素,然后将鼠标悬停在目标上。

这是执行脚本后容器的外观: After execution

谢谢。

2 个答案:

答案 0 :(得分:3)

问题是因为我正在使用drag_and_drop命令。

通过以下命令,拖放功能现在可以在Chrome和Firefox上使用:

    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

浏览器设置和驱动程序版本是我所遇到的问题。请注意,我还用XPath交换了CSS,以防影响元素的选择。

希望这会有所帮助!

答案 1 :(得分:0)

此代码可完美运行

element = driver.find_element_by_xpath('source xpath')
target =  driver.find_element_by_xpath('destination xpath')

ActionChains(driver).drag_and_drop(element, target).perform()