0

I have a table with 3 rows (tr's) and 3 columns (td cells).

About this table:

1.The columns are not fixed, and 1st column can be moved to 2nd column.

2.The manager column just contains a round box like img which will be checked (only one at a time).

Name Email Manager

Name1 email_id_1 roundbox (selected currently)

Name2 email_id_2 roundbox

Name3 email_id_3 roundbox

I was successful in getting the number of rows, number of columns, index of each column from the table.

List<WebElement> rows = tablelocator.findElements(By.path of rows locator)
print(rows.size)

But the next step is failing where in the element "Xpath of the selected manager" is not visible (not time / wait related issue).

WebElement rowWhichIWant = driver.findElement(By.xpath("path of row 1 whose manager is selected"));

The above fails with no such element (even though the row gets highlighted in the browser when searching with this locator, so its not a locator problem as well)

I want to retrieve only the email id of the currently selected manager, i.e email_id_1.

HTML:

<div tabindex="0" id="somename">
      <table class="x1ns" summary="some summary">
         <colgroup span="3">
            <col style="width:161px;">
            <col style="width: 210px;">
            <col style="width: 310px;">
         </colgroup>
         <tbody>
            <tr>
               <th style="padding:0px;padding-left:11px;width:150px;"></th>
               <th style="padding: 0px 0px 0px 11px; width: 199px;"></th>
               <th style="padding: 0px 0px 0px 11px; width: 299px;"></th>

            </tr>
            <tr>
               <th id="someid">
                  <div style="some style">
                     <table id="some id">
                        <tbody>
                           <tr>
                              <td _afrsortasc="1"><a tabindex="-1" class="x1nw" title="Sort Ascending"></a></td>
                              <td _afrsortdesc="1"><a tabindex="-1" class="x1nx" title="Sort Descending"></a></td>
                           </tr>
                        </tbody>
                     </table>
                  </div>
                  <div class="x1zr"><span class="af_column_label-text">Name</span></div>
               </th>
               <th id="someid">
                  <div style="some style">
                     <table id="some id">
                        <tbody>
                           <tr>
                              <td _afrsortasc="1"><a tabindex="-1" class="x1nw" title="Sort Ascending"></a></td>
                              <td _afrsortdesc="1"><a tabindex="-1" class="x1nx" title="Sort Descending"></a></td>
                           </tr>
                        </tbody>
                     </table>
                  </div>
                  <div class="x1zr"><span class="af_column_label-text">E-Mail</span></div>
               </th>          
               <th id="someid">
                  <div style="some style">
                     <table id="some id">
                        <tbody>
                           <tr>
                              <td _afrsortasc="1"><a tabindex="-1" class="x1nw" title="Sort Ascending"></a></td>
                              <td _afrsortdesc="1"><a tabindex="-1" class="x1nx" title="Sort Descending"></a></td>
                           </tr>
                        </tbody>
                     </table>
                  </div>
                  <div class="x1zr"><span class="af_column_label-text">Manager</span></div>
               </th>
            </tr>
         </tbody>
      </table>
   </div>
   <div id="some id">
      <table summary="some summary" class="x1nj x1o7">
         <colgroup span="3">
            <col style="width:161px;">
            <col style="width: 210px;">
            <col style="width: 310px;">
         </colgroup>
         <tbody>
            <tr _afrrk="0" class="xep">
               <td style="width: 199px;" class="xeq" nowrap=""><a id="someid1" class="someclass1" href="mailto:[email protected]">[email protected]</a></td>
               <td style="width: 299px;" class="xeq" nowrap=""><a id="someid2" class="someclass2" onclick="this.focus();return false;" href="#">Name1</a></td>
               <td style="width:90px;" class="xeq" nowrap="" align="center"><img id="someimgid1" title="Primary" alt="Primary" class="xje"></td>
            </tr>
            <tr _afrrk="1" class="xep">
               <td class="xeq" nowrap=""><a id="someid3" class="someclass3" href="mailto:[email protected]">[email protected]</a></td>
               <td class="xeq" nowrap=""><a id="someid4" class="someclass4" onclick="this.focus();return false;" href="#">Name2</a></td>
               <td class="xeq" nowrap="" align="center"><a id="someaid2" title="Primary" class="xko p_AFIconOnly" onclick="this.focus();return false;" href="#"><img id="someimgid2" title="Primary" alt="Primary" class="_afrImageNotLoadedInTime xi8"></a></td>

            </tr>
            <tr _afrrk="2" class="xep">
               <td class="xeq" nowrap=""><a id="someid3" class="someclass3" href="mailto:[email protected]">[email protected]</a></td>
               <td class="xeq" nowrap=""><a id="someid4" class="someclass4" onclick="this.focus();return false;" href="#">Name3</a></td>
               <td class="xeq" nowrap="" align="center"><a id="someaid3" title="Primary" class="xko p_AFIconOnly" onclick="this.focus();return false;" href="#"><img id="someimgid3" title="Primary" alt="Primary" class="xi8"></a></td>
            </tr>            
         </tbody>
      </table>
   </div>
   <div id="someid123" style="height: 0px; width: 1040px;"></div>
   <div id="someid234" class="x1ob" style="position:absolute;display:none"></div>
   <div id="someid345" class="x1nk" style="position:absolute;display:none;overflow:hidden"></div>
   <div id="someid456" style="display:none"></div>
   <div tabindex="-1" id="someid567" style="position: absolute; overflow: auto; z-index: 0; width: 1040px; top: 33px; height: 261px; right: 0px;">
      <div style="width: 1040px; height: 223px; visibility: hidden;"></div>
   </div>

2
  • Please provide HTML code Commented Aug 18, 2019 at 7:47
  • Provided HTML in edit. Please help. Commented Aug 18, 2019 at 12:08

1 Answer 1

1

no such element indicates that indeed the element was not found, You can try this code to get all the text from the table :

new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@class,'x1nj')]")));
WebElement tbl = driver.findElement(By.xpath("//*[contains(@class,'x1nj')]"));
List<WebElement> rows = tbl.findElements(By.tagName("tr"));

for(int i=0; i<rows.size(); i++) {
    //check column each in row, identification with 'td' tag
    List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));

    //column iteration
    for(int j=0; j<cols.size(); j++) {
        System.out.println("row " +(i+1) +" col " +(j+1) +" : " +cols.get(j).getText());
    }
}

Console output :

console output

So this is to get the cell you want :

new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@class,'x1nj')]")));
WebElement tbl = driver.findElement(By.xpath("//*[contains(@class,'x1nj')]"));
List<WebElement> rows = tbl.findElements(By.tagName("tr"));
List<WebElement> cols = rows.get(0).findElements(By.tagName("td"));
WebElement cell1 = cols.get(0);
WebElement cell2 = cols.get(1);
System.out.println("get text : " +cell1.getText());
System.out.println("get text : " +cell2.getText());

Reference

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Frian. But my question is a bit different.
As per your code, after clicking the cellYouWant, how do I get the text from it's corresponding column (i.e name or email id)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.