【问题标题】:Selenium how to get cell value in a grid based on grid coordinatesSelenium如何根据网格坐标获取网格中的单元格值
【发布时间】:2021-07-08 15:30:15
【问题描述】:

HTML

<div id="model" class="mobile handsontable htRowHeaders htColumnHeaders" style="height: 100%; overflow: hidden;" data-originalstyle="height: 100%; overflow: hidden;">
  <div class="ht_master handsontable"><div class="wtHolder" style="position: relative; height: 380px; width: 1468px;">
  <div class="wtHider" style="width: 2550px; height: 2109px;">
    <div class="wtSpreader" style="position: relative; top: 0px; left: 0px;">
      <table class="htCore">
        <colgroup>
          <col class="rowHeader" style="width: 50px;">
          <col style="width: 400px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
          <col style="width: 70px;">
        </colgroup>
        <thead>
          <tr>
            <th class="">
              <div class="relative">
                <span class="colHeader cornerHeader">&nbsp;</span>
              </div>
            </th>
            <th class=""> 
              <div class="relative">
                <span class="colHeader">Division, Base Alternative</span>
              </div>
             </th>
            <th class="">
              <div class="relative">
                <span class="colHeader">2015 (M)</span>
              </div>
             </th>
            <th class="">
              <div class="relative">
                <span class="colHeader">2016 (N)</span>
              </div>
             </th>
            <th class="">
              <div class="relative">
                <span class="colHeader">2017 (O)</span>
              </div>
             </th>
            <th class="">
              <div class="relative">
                <span class="colHeader">2018 (P)</span>
              </div>
             </th>
            <th class="">
              <div class="relative">
                <span class="colHeader">2019 (Q)</span>
              </div>
            </th>
           </tr>
          <thead>
            <tr>                                                                                                     <th class="" style="height: 17px;">
                <div class="relative">
                  <span class="rowHeader">1</span>
                </div>
                </th>
              <td class="htDimmed htNoWrap"></td>
              <td class="afterHiddenColumn htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
              <td class="htDimmed htNoWrap"></td>
            <tr>
              <th class="" style="height: 17px;">
                <div class="relative">
                  <span class="rowHeader">2</span>
                </div>
               </th>
              <td class="htDimmed htNoWrap" title="" style="background-color: rgb(194, 218, 254); font-size: 10pt; color: rgb(0, 0, 0); text-align: left;"></td>
              <td class="afterHiddenColumn htDimmed htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;"></td>
              <td class="htDimmed htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;"></td>
              <td class="htDimmed htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;"></td>
              <td class="htDimmed htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;"></td>
              <td class="htDimmed htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;"></td>
            </tr>
           <tr>
            <th class="" style="height: 17px;">
              <div class="relative">
                <span class="rowHeader">3</span>
              </div>
             </th>
            <td class="htNoWrap" title="" style="background-color: rgb(194, 218, 254); font-size: 10pt; color: rgb(0, 0, 0); text-align: left;">Model period</td>
            <td class="afterHiddenColumn htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
            <td class="htRight htNumeric htNoWrap" title="" style="background-color: rgb(255, 255, 128); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
          </tr>         
        <tr>
          <th class="" style="height: 17px;">
            <div class="relative">
              <span class="rowHeader">7</span>
            </div>
           </th>
          <td class="htNoWrap" title="" style="background-color: rgb(194, 218, 254); font-size: 10pt; color: rgb(0, 0, 0); text-align: left;">Pulp sales, pulp mill, MUSD</td>
          <td class="afterHiddenColumn htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">597</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">572</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">648</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">35</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">326</td>
          <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">326</td>
        </tr>
       <tr>
        <th class="" style="height: 17px;">
          <div class="relative">
            <span class="rowHeader">8</span>
          </div>
         </th>
        <td class="htNoWrap" title="" style="background-color: rgb(194, 218, 254); font-size: 10pt; color: rgb(0, 0, 0); text-align: left;">Pulp sales, paper mill, MUSD</td>
        <td class="afterHiddenColumn htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
        <td class="htNoWrap" title="" style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(0, 0, 0); text-align: center;">0</td>
       </tr>
      </thead>
     
  

我想在如下所示的网格中获取特定单元格
基于左侧的行号和顶部的列名。

假设我想获取值为597的单元格

而不是使用值/文本或索引来查找它,
我想根据row number & column name 获取单元格

类似的东西

找到行号为 7 列名为 2017 (O) 的单元格

预期输出

597

如果选择了行或列
他们得到一个名为ht__highlight

的类

如果单元格被选中
它有一个名为 htNoWrap current highlight

的类

我已经搜索了 2 天,
我找不到任何答案或 这种情况的例子

这很难解释,所以我为混乱的解释道歉

提前致谢!!!!

【问题讨论】:

  • 你把一切都复杂化了。只需为页面添加 html 代码。
  • 我将添加定位器的外观,但如果没有完整的 HTML,很难确定

标签: python selenium selenium-webdriver html-table


【解决方案1】:

方法应该是这样的:

rows = driver.find_elements_by_css_selector(".rowHeader") 
for row in rows:
    row_2015 = row.find_element_by_css_selector(".colHeader:nth-of-type(1)").text
    row_2016 = row.find_element_by_css_selector(".colHeader:nth-of-type(2)").text
  1. 您可以通过查找唯一的行定位符来获取所有行(例如,对于 15 行 - 15 个结果)
  2. 循环遍历表格行并获取单个单元格文本。

换句话说,你的 css 选择器应该是这样的:

expected_text = driver.find_element_by_css_selector(".rowHeader:nth-of-type(1)>.colHeader:nth-of-type(1)").text

没有完整的 HTML 页面,很难确定。

尝试 2:

根据您指定的条件使用以下xpath

//span[contains(text(),'2017 (O)')]/../../../../../thead[2]/tr[4]/td[4]

它通过列名直接定位值为 597 的单元格。 首先它找到列名,然后再向上几级以从第二个thead 获取值。

尝试更改 tr[4]/td[4] 部分,您将通过 xpath 从表中获取任何值。

【讨论】:

  • 我应该用什么来代替“nth-of-type”?我收到 UnableToLocateElement 错误
  • 我已经更新了我的问题。请看一看!
  • 问题还是一样,你想取值为 597 的单元格吗?还是有其他条件?
  • 是的,仍然是值为 597 的单元格,但根据行号和列名坐标定位它。 @vitaliis
  • //tr[{}]//td[count(//table[@class='htCore']//th[contains(., '{}')]//preceding-sibling::th
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多