1

On selecting the radio button how to get the text of the selected radio button where the text is in the next column

I randomly select the radio button and then i need to Store the selected Radio button's Vessel Name

Below is the Code:

public   String checkselectedplanningVesselname()
    {
        String rowOne = "";
            
            List<WebElement> radiobuttonoptions =driver.findElements(By.xpath("//table[contains(@class,'mat-table cdk-table')]//following::mat-radio-button[contains(@id,'mat-radio')]"));
             for (int i=0;i<radiobuttonoptions.size();i++)
            if(driver.findElement(By.xpath("//table[contains(@class,'mat-table cdk-table')]//following::mat-radio-button[contains(@id,'mat-radio')]")).isSelected())
            {
             
            
             rowOne = radiobuttonoptions.get(i).getText(); = 
             System.out.println("Print the Selected VesselName:"+ rowOne);
           break;
         
        }
        return rowOne;
    }

Below is the HTML of the table :

HTML:

    <tbody role="rowgroup">
       <tr _ngcontent-epp-c110="" role="row" mat-row="" class="mat-row cdk-row ng-star-inserted">
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-radio mat-column-radio ng-star-inserted">
             <span _ngcontent-epp-c110="" class="ng-star-inserted">
                <!---->
                <mat-radio-button _ngcontent-epp-c110="" color="primary" class="mat-radio-button mat-primary ng-star-inserted" id="mat-radio-1"><label class="mat-radio-label" for="mat-radio-1-input"><span class="mat-radio-container"><span class="mat-radio-outer-circle"></span><span class="mat-radio-inner-circle"></span><input type="radio" class="mat-radio-input cdk-visually-hidden" id="mat-radio-1-input" tabindex="0" value="8252"><span mat-ripple="" class="mat-ripple mat-radio-ripple mat-focus-indicator"><span class="mat-ripple-element mat-radio-persistent-ripple"></span></span></span><span class="mat-radio-label-content"><span style="display: none;">&nbsp;</span></span></label></mat-radio-button>
                <!----><!---->
             </span>
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted"></span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vesselName mat-column-vesselName ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TESTVESSELTECHM1</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vettingStatus mat-column-vettingStatus ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">GREEN</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-imoNumber mat-column-imoNumber ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">00121</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vouageId mat-column-vouageId ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TBD</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-lastValidityDate mat-column-lastValidityDate ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">2035-10-05</span><!---->
          </td>
          <!---->
       </tr>
       <tr _ngcontent-epp-c110="" role="row" mat-row="" class="mat-row cdk-row ng-star-inserted">
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-radio mat-column-radio ng-star-inserted">
             <span _ngcontent-epp-c110="" class="ng-star-inserted">
                <!---->
                <mat-radio-button _ngcontent-epp-c110="" color="primary" class="mat-radio-button mat-primary ng-star-inserted mat-radio-checked" id="mat-radio-2"><label class="mat-radio-label" for="mat-radio-2-input"><span class="mat-radio-container"><span class="mat-radio-outer-circle"></span><span class="mat-radio-inner-circle"></span><input type="radio" class="mat-radio-input cdk-visually-hidden" id="mat-radio-2-input" tabindex="0" value="8253"><span mat-ripple="" class="mat-ripple mat-radio-ripple mat-focus-indicator"><span class="mat-ripple-element mat-radio-persistent-ripple"></span></span></span><span class="mat-radio-label-content"><span style="display: none;">&nbsp;</span></span></label></mat-radio-button>
                <!----><!---->
             </span>
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted"></span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vesselName mat-column-vesselName ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TESTVESSELTECHM2</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vettingStatus mat-column-vettingStatus ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">GREEN</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-imoNumber mat-column-imoNumber ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">00122</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vouageId mat-column-vouageId ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TBD</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-lastValidityDate mat-column-lastValidityDate ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">2035-10-15</span><!---->
          </td>
          <!---->
       </tr>
       <tr _ngcontent-epp-c110="" role="row" mat-row="" class="mat-row cdk-row ng-star-inserted">
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-radio mat-column-radio ng-star-inserted">
             <span _ngcontent-epp-c110="" class="ng-star-inserted">
                <!---->
                <mat-radio-button _ngcontent-epp-c110="" color="primary" class="mat-radio-button mat-primary ng-star-inserted" id="mat-radio-3"><label class="mat-radio-label" for="mat-radio-3-input"><span class="mat-radio-container"><span class="mat-radio-outer-circle"></span><span class="mat-radio-inner-circle"></span><input type="radio" class="mat-radio-input cdk-visually-hidden" id="mat-radio-3-input" tabindex="0" value="8301"><span mat-ripple="" class="mat-ripple mat-radio-ripple mat-focus-indicator"><span class="mat-ripple-element mat-radio-persistent-ripple"></span></span></span><span class="mat-radio-label-content"><span style="display: none;">&nbsp;</span></span></label></mat-radio-button>
                <!----><!---->
             </span>
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted"></span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vesselName mat-column-vesselName ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TESTVESSELTECHM1</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vettingStatus mat-column-vettingStatus ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">GREEN</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-imoNumber mat-column-imoNumber ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">00121</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-vouageId mat-column-vouageId ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TBD</span><!---->
          </td>
          <td _ngcontent-epp-c110="" role="gridcell" mat-cell="" class="mat-cell cdk-cell cdk-column-lastValidityDate mat-column-lastValidityDate ng-star-inserted">
             <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">2035-10-25</span><!---->
          </td>
          <!---->
       </tr>
       <!----><!---->
    </tbody>

9
  • Hi your html of the table is not coorect , could you add the full table element]
    – PDHide
    Sep 21, 2021 at 7:49
  • @PDHide - Have added the entire table html but is not showing fully
    – jay m
    Sep 21, 2021 at 7:52
  • please make sure to use diffwerent formatters when creating question , use code formatter to show code and html tags
    – PDHide
    Sep 21, 2021 at 7:54
  • @PDHide - got ok any inputs on this
    – jay m
    Sep 21, 2021 at 7:55
  • the locator is not available in the html you provided
    – PDHide
    Sep 21, 2021 at 8:01

2 Answers 2

0

use xpath to select the entire row , and select individual td item using index

//tr[@role="row"][2]/td[3]

so for selecting radio button of first

//tr[@role="row"]/td[1]

to get text :

//tr[@role="row"]/td[1]
3
  • -I have already selected the radio button over here my only question is i need to get the text of the selected Radio button and the radio button in this case will be randomly selected i could be either first or second etc so in this case as seen above after entering the loop what i need to do in order to get the text that is my question - @PDHide
    – jay m
    Sep 21, 2021 at 8:33
  • @PDHide- thank you
    – jay m
    Sep 21, 2021 at 12:57
  • You should navigate to parent element , but it's like going around the field. Instead select the row you want and then select radio , text etc from that row
    – PDHide
    Sep 21, 2021 at 13:28
0

I tried this code [Selenium + Java] with the given HTML and it worked for me:

String rowOne = "";
String radioButton = "";
            
List<WebElement> radiobuttonoptions =driver.findElements(By.xpath("//mat-radio-button[contains(@id,'mat-radio')]"));
for (WebElement radiobuttonoption : radiobuttonoptions)
{
    radioButton = radiobuttonoption.getAttribute("id");
    radioButton = "//mat-radio-button[contains(@id,'" + radioButton + "')]";
    driver.findElement(By.xpath(radioButton)).click();
    rowOne = driver.findElement(By.xpath(radioButton + "//ancestor::tr/td[2]/span")).getText();
        System.out.println("Selected button is: "+ radioButton);
        System.out.println("Print the Selected VesselName:"+ rowOne);
        Thread.sleep(2000);
}

This is the HTML I used:

<html>

<body>
   <table class="mat-table cdk-table">
      <tbody role="rowgroup">
         <tr _ngcontent-epp-c110="" role="row" mat-row="" class="mat-row cdk-row ng-star-inserted">
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-radio mat-column-radio ng-star-inserted">
               <span _ngcontent-epp-c110="" class="ng-star-inserted">
                  <!---->
                  <mat-radio-button _ngcontent-epp-c110="" color="primary"
                     class="mat-radio-button mat-primary ng-star-inserted" id="mat-radio-1"><label
                        class="mat-radio-label" for="mat-radio-1-input"><span class="mat-radio-container"><span
                              class="mat-radio-outer-circle"></span><span class="mat-radio-inner-circle"></span><input
                              type="radio" class="mat-radio-input cdk-visually-hidden" id="mat-radio-1-input"
                              tabindex="0" value="8252"><span mat-ripple=""
                              class="mat-ripple mat-radio-ripple mat-focus-indicator"><span
                                 class="mat-ripple-element mat-radio-persistent-ripple"></span></span></span><span
                           class="mat-radio-label-content"><span style="display: none;">&nbsp;</span></span></label>
                  </mat-radio-button>
                  <!---->
                  <!---->
               </span>
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted"></span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vesselName mat-column-vesselName ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TESTVESSELTECHM1</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vettingStatus mat-column-vettingStatus ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">GREEN</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-imoNumber mat-column-imoNumber ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">00121</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vouageId mat-column-vouageId ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TBD</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-lastValidityDate mat-column-lastValidityDate ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">2035-10-05</span>
               <!---->
            </td>
            <!---->
         </tr>
         <tr _ngcontent-epp-c110="" role="row" mat-row="" class="mat-row cdk-row ng-star-inserted">
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-radio mat-column-radio ng-star-inserted">
               <span _ngcontent-epp-c110="" class="ng-star-inserted">
                  <!---->
                  <mat-radio-button _ngcontent-epp-c110="" color="primary"
                     class="mat-radio-button mat-primary ng-star-inserted mat-radio-checked" id="mat-radio-2"><label
                        class="mat-radio-label" for="mat-radio-2-input"><span class="mat-radio-container"><span
                              class="mat-radio-outer-circle"></span><span class="mat-radio-inner-circle"></span><input
                              type="radio" class="mat-radio-input cdk-visually-hidden" id="mat-radio-2-input"
                              tabindex="0" value="8253"><span mat-ripple=""
                              class="mat-ripple mat-radio-ripple mat-focus-indicator"><span
                                 class="mat-ripple-element mat-radio-persistent-ripple"></span></span></span><span
                           class="mat-radio-label-content"><span style="display: none;">&nbsp;</span></span></label>
                  </mat-radio-button>
                  <!---->
                  <!---->
               </span>
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted"></span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vesselName mat-column-vesselName ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TESTVESSELTECHM2</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vettingStatus mat-column-vettingStatus ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">GREEN</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-imoNumber mat-column-imoNumber ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">00122</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vouageId mat-column-vouageId ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TBD</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-lastValidityDate mat-column-lastValidityDate ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">2035-10-15</span>
               <!---->
            </td>
            <!---->
         </tr>
         <tr _ngcontent-epp-c110="" role="row" mat-row="" class="mat-row cdk-row ng-star-inserted">
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-radio mat-column-radio ng-star-inserted">
               <span _ngcontent-epp-c110="" class="ng-star-inserted">
                  <!---->
                  <mat-radio-button _ngcontent-epp-c110="" color="primary"
                     class="mat-radio-button mat-primary ng-star-inserted" id="mat-radio-3"><label
                        class="mat-radio-label" for="mat-radio-3-input"><span class="mat-radio-container"><span
                              class="mat-radio-outer-circle"></span><span class="mat-radio-inner-circle"></span><input
                              type="radio" class="mat-radio-input cdk-visually-hidden" id="mat-radio-3-input"
                              tabindex="0" value="8301"><span mat-ripple=""
                              class="mat-ripple mat-radio-ripple mat-focus-indicator"><span
                                 class="mat-ripple-element mat-radio-persistent-ripple"></span></span></span><span
                           class="mat-radio-label-content"><span style="display: none;">&nbsp;</span></span></label>
                  </mat-radio-button>
                  <!---->
                  <!---->
               </span>
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted"></span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vesselName mat-column-vesselName ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TESTVESSELTECHM1</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vettingStatus mat-column-vettingStatus ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">GREEN</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-imoNumber mat-column-imoNumber ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">00121</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-vouageId mat-column-vouageId ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">TBD</span>
               <!---->
            </td>
            <td _ngcontent-epp-c110="" role="gridcell" mat-cell=""
               class="mat-cell cdk-cell cdk-column-lastValidityDate mat-column-lastValidityDate ng-star-inserted">
               <!----><span _ngcontent-epp-c110="" class="ng-star-inserted">2035-10-25</span>
               <!---->
            </td>
            <!---->
         </tr>
         <!---->
         <!---->
      </tbody>
   </table>
</body>

</html>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.