Python Selenium get cell value based on coordinates

I want to find a specific cell in a table based on coordinates.

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>

Picture of the original grid table
enter image description here

I have tried a bunch of different methods, but I can’t understand how to do it.

I want to find a cell, based on line 7 & column name 2017 (O) for example, and get its value

The only example I find, is to find a cell by its index or by its value, that’s not what I want to accomplish.

Thanks in advance!

EDIT
I have provided more HTML code.

Answer

Use the following xpath to get the position of the column index based on header text.

count(//table[@class='htCore']//th[contains(., '{}')]/preceding-sibling::th)

You might need to adjust position based on structure like

count(//table[@class='htCore']//th[contains(., '{}')]/preceding-sibling::th)+1

Use following function to get the dynamic value based on the row number and cell header you want.

def Get_Cellvalue(rownum, colheader):
   element=driver.find_element_by_xpath("//tr[{}]//td[count(//table[@class='htCore']//th[contains(., '{}')]/preceding-sibling::th)]".format(rownum,colheader))
   print(element.text)

Get_Cellvalue(7,"2017 (O)") 
Get_Cellvalue(7,"2018 (P)")  
Get_Cellvalue(7,"2019 (Q)")

Hope this will solved your task.