在 table python selenium 中按顺序拖动行

Drag rows in order in a table python selenium

我有一个html这样的

Link 到 html

http://threedonthemap.s3-website.ap-south-1.amazonaws.com/


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Easy Drag and Drop HTML Table Rows With jQuery</title>
    <!-- Bootstrap core CSS -->
    <link href="https://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="https://getbootstrap.com/docs/4.0/examples/starter-template/starter-template.css" rel="stylesheet">
  </head>

  <body>

    <main role="main" class="container">

     <table class="table table-striped table-hover">
        <thead class="thead-dark">
            <tr>
                <th>Id</th>
                <th>Jabcode</th>
            </tr>
        </thead>
        <tbody>
            <tr>
              <td>01</td>
              <td>E24.9</td>
            </tr>
            <tr>
              <td>02</td>
              <td>J92.9</td>
            </tr>
            <tr>
              <td>03</td>
              <td>A10.2</td>
            </tr>
            <tr>
              <td>04</td>
              <td>B10.2</td>
            </tr>
            <tr>
              <td>05</td>
              <td>C4.9</td>
            </tr>
            <tr>
              <td>06</td>
              <td>D10.11</td>

            </tr>
            <tr>
              <td>07</td>
              <td>F19.10</td>             
            </tr>

        </tbody>
    </table>

    </main><!-- /.container -->

    <!-- Bootstrap & Core Scripts -->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> 
    <script src="https://getbootstrap.com/dist/js/bootstrap.min.js"></script>

    <script type="text/javascript">
      $('tbody').sortable();
    </script>

  </body>
</html>

当前在网页中有特定顺序

我想根据我在 python 程序中提供的参数更改网页中项目的顺序。

这是我的 python 代码

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time 

jab_code_order =['J92.9','A10.2','E24.9','B10.2','F19.10','D10.11','C4.9']
    # browser = 

driver = webdriver.Chrome('/chromedrivernew')
driver.get("file:////datasets/demodragable/index.html")





for item in jab_code_order:
    cell=driver.find_element_by_xpath("//tr[@class='ui-sortable-handle']//td[text()='"+ item + "']")
    print(cell.text)
    source_element = driver.find_element_by_xpath("//tr[@class='ui-sortable-handle']//td[text()='"+ item + "']")
    dest_element = driver.find_element_by_xpath("//tr[@class='ui-sortable-handle']//td[text()='A10.2']")
    ActionChains(driver).drag_and_drop(source_element, dest_element).perform()
    time.sleep(2)

当前代码是这样的

我想要 table 这样的订单

有什么 pythonic 方法可以代替循环吗?

如有任何帮助,我们将不胜感激!!

这是面试题吗?哈哈你还给我一些吗?

这就是问题所在,此页面上的 drag_and_drop 仅在您所需的目的地位于来源下方时才有效。我使用 drag_and_drop_by_offset 来解决这个问题并添加 5 个像素。请参阅此处的文档 (https://selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html) 还请记住,每次移动元素时,列表的顺序都会发生变化。我添加了一个新函数来为循环的每次迭代查找该页面上的所有元素,以解决该问题。 见下文:

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time 

jab_code_order =['J92.9','A10.2','E24.9','B10.2','F19.10','D10.11','C4.9']
    # browser = 

driver = webdriver.Chrome('C:\Path\To\chromedriver.exe')
driver.get("file://C:\Path\To\index.html")


def get_current_element_order():
    array_of_elements = driver.find_elements_by_xpath("//tbody//tr")
    return array_of_elements

for item in range(len(jab_code_order)):
    cell=driver.find_element_by_xpath("//tr[@class='ui-sortable-handle']//td[text()='"+ jab_code_order[item] + "']")
    print(cell.text)
    source_element = driver.find_element_by_xpath("//tr[@class='ui-sortable-handle']//td[text()='"+ jab_code_order[item] + "']")
    current_order_of_elements = get_current_element_order()
    dest_element = current_order_of_elements[item]
    if dest_element.location['y'] - source_element.location['y'] < 0:
        ActionChains(driver).drag_and_drop_by_offset(source_element,
                                                     0,
                                                     dest_element.location['y'] - source_element.location['y'] - 5).perform()
    else:
        ActionChains(driver).drag_and_drop_by_offset(source_element,
                                                     0,
                                                     dest_element.location['y'] - source_element.location['y']).perform()
    time.sleep(2)