【问题标题】:Drag rows in order in a table python selenium在表格中按顺序拖动行 python selenium
【发布时间】:2020-04-22 13:30:39
【问题描述】:

我有一个这样的html

链接到 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)

现在的代码是这样的

我想要这样的餐桌顺序

是否有任何 Pythonic 方式而不是循环?

任何帮助将不胜感激!

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver selenium-ide


    【解决方案1】:

    这是一个面试问题,哈哈?你有更多的给我吗?

    这是问题所在,此页面上的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)
    

    【讨论】:

    • 很高兴为您提供帮助,请记住文档是您的朋友。 selenium.dev/selenium/docs/api/py/index.html
    • 您能看看我对文件路径所做的更改吗?
    • @user9538877 我觉得那个文件路径file:////index.html中的斜杠太多了。
    • 执行:local_file = "C:\\Path\\To\\local\\index.html" 然后driver.get('file://' + local_file)
    • 在同一个 senerio 而不是 jabcode 我们如何通过 id 移动元素?
    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2019-12-20
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多