Я пытаюсь перетащить, используя Python и Selenium.
У меня есть два контейнера со строками, которые указывают имена файлов, и перетаскиванием я хочу перенести один файл из первого контейнера во второй..
Вот так выглядят контейнеры:
Я видел в Интернете, что у библиотек селена и перетаскивания есть некоторые проблемы, и я не совсем уверенкак сделать эту работу.
Поскольку версии браузеров и драйверов играют важную роль, пожалуйста, обратите внимание на те, которые я использую:
Способ, которым я пытаюсьперетаскивание выглядит следующим образом:
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
HTML-код контейнера1:
<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"> </td></tr></tbody>
</table>
</div>
HTML-код контейнера2:
<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"> </td></tr></tbody>
</table>
</div>
Может кто-нибудь помочь мне решить эту проблему с помощью Python?Я пытался с обоими драйверами, и оба терпят неудачу.Я не получаю никаких исключений, но действие не работает.
В результате драйвер нажимает на элемент источника и наводит курсор на цель.
Вот как выглядят контейнеры после выполнения скрипта:
Спасибо.