How to identify the last row and then click on the Add button..With given suggestions by people it clicks second rowwant to click on LastRow any Leads

I have a table where the rows will get added based on the inputs from the other screen. The Question here is how to identify the no of rows added and always click on the add button from the Last Row.

This is my code i have taken a List of WebElement of rows but not sure how to always click on the last row add button .

List<WebElement> noofrows = driver.findElements(By.xpath("//div[contains(@class,'form-tab-name') and contains(normalize-space(),'Partners')]/following::div[contains(@class,'ng-untouched')]"));
int rowcount = noofrows.size();

After this what I need to do in order to always click on the last row add button. Can someone please give the inputs for this?

HTML:

<div _ngcontent-lkf-c122="" class="ng-untouched ng-pristine ng-star-inserted ng-valid"><div _ngcontent-lkf-c122="" class="form-tab-name form-tab-name-space"> Partners </div><div _ngcontent-lkf-c122="" class="ng-untouched ng-pristine ng-valid ng-star-inserted"><section _ngcontent-lkf-c122="" class="resp-grid-partners"><div _ngcontent-lkf-c122="" class="ng-star-inserted"><mat-form-field _ngcontent-lkf-c122="" appearance="outline" class="mat-form-field form-field ng-tns-c92-234 mat-primary mat-form-field-type-mat-select mat-form-field-appearance-outline mat-form-field-can-float mat-form-field-has-label ng-untouched ng-pristine ng-valid mat-form-field-should-float"><div class="mat-form-field-wrapper ng-tns-c92-234"><div class="mat-form-field-flex ng-tns-c92-234"><div class="mat-form-field-outline ng-tns-c92-234 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-234" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-234" style="width: 74.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-234"></div></div><div class="mat-form-field-outline mat-form-field-outline-thick ng-tns-c92-234 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-234" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-234" style="width: 74.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-234"></div></div><!----><!----><!----><div class="mat-form-field-infix ng-tns-c92-234"><mat-select _ngcontent-lkf-c122="" role="combobox" aria-autocomplete="none" aria-haspopup="true" class="mat-select ng-tns-c95-237 mat-select-required ng-untouched ng-pristine ng-valid ng-star-inserted" aria-labelledby="mat-form-field-label-381 mat-select-value-43" id="mat-select-42" tabindex="0" aria-expanded="false" aria-required="true" aria-disabled="false" aria-invalid="false" required=""><div cdk-overlay-origin="" class="mat-select-trigger ng-tns-c95-237"><div class="mat-select-value ng-tns-c95-237" id="mat-select-value-43"><!----><span class="mat-select-value-text ng-tns-c95-237 ng-star-inserted"><span class="mat-select-min-line ng-tns-c95-237 ng-star-inserted">CHARTERER</span><!----><!----></span><!----></div><div class="mat-select-arrow-wrapper ng-tns-c95-237"><div class="mat-select-arrow ng-tns-c95-237"></div></div></div><!----></mat-select><!----><!----><span class="mat-form-field-label-wrapper ng-tns-c92-234"><label class="mat-form-field-label ng-tns-c92-234 ng-star-inserted" id="mat-form-field-label-381" for="mat-select-42" aria-owns="mat-select-42"><!----><mat-label _ngcontent-lkf-c122="" class="ng-tns-c92-234 ng-star-inserted"> Partner Role </mat-label><!----><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c92-234 ng-star-inserted"> *</span><!----></label><!----></span></div><!----></div><!----><div class="mat-form-field-subscript-wrapper ng-tns-c92-234"><!----><div class="mat-form-field-hint-wrapper ng-tns-c92-234 ng-trigger ng-trigger-transitionMessages ng-star-inserted" style="opacity: 1; transform: translateY(0%);"><!----><div class="mat-form-field-hint-spacer ng-tns-c92-234"></div></div><!----></div></div></mat-form-field></div><div _ngcontent-lkf-c122="" class="ng-star-inserted"><mat-form-field _ngcontent-lkf-c122="" appearance="outline" class="mat-form-field form-field ng-tns-c92-235 mat-primary mat-form-field-type-mat-input mat-form-field-appearance-outline mat-form-field-can-float mat-form-field-should-float mat-form-field-has-label ng-untouched ng-pristine ng-valid"><div class="mat-form-field-wrapper ng-tns-c92-235"><div class="mat-form-field-flex ng-tns-c92-235"><div class="mat-form-field-outline ng-tns-c92-235 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-235" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-235" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-235"></div></div><div class="mat-form-field-outline mat-form-field-outline-thick ng-tns-c92-235 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-235" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-235" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-235"></div></div><!----><!----><!----><div class="mat-form-field-infix ng-tns-c92-235"><!----><input _ngcontent-lkf-c122="" matinput="" class="mat-input-element mat-form-field-autofill-control ng-untouched ng-pristine ng-valid cdk-text-field-autofill-monitored ng-star-inserted" type="text" required="" id="partnerName" aria-invalid="false" aria-required="true" maxlength="100"><!----><span class="mat-form-field-label-wrapper ng-tns-c92-235"><label class="mat-form-field-label ng-tns-c92-235 ng-star-inserted" id="mat-form-field-label-383" for="partnerName" aria-owns="partnerName"><!----><mat-label _ngcontent-lkf-c122="" class="ng-tns-c92-235 ng-star-inserted"> Partner name </mat-label><!----><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c92-235 ng-star-inserted"> *</span><!----></label><!----></span></div><!----></div><!----><div class="mat-form-field-subscript-wrapper ng-tns-c92-235"><!----><div class="mat-form-field-hint-wrapper ng-tns-c92-235 ng-trigger ng-trigger-transitionMessages ng-star-inserted" style="opacity: 1; transform: translateY(0%);"><!----><div class="mat-form-field-hint-spacer ng-tns-c92-235"></div></div><!----></div></div></mat-form-field></div><div _ngcontent-lkf-c122="" class="ng-star-inserted"><mat-form-field _ngcontent-lkf-c122="" appearance="outline" class="mat-form-field form-field ng-tns-c92-236 mat-primary mat-form-field-type-mat-input mat-form-field-appearance-outline mat-form-field-can-float mat-form-field-should-float mat-form-field-has-label ng-untouched ng-pristine ng-valid"><div class="mat-form-field-wrapper ng-tns-c92-236"><div class="mat-form-field-flex ng-tns-c92-236"><div class="mat-form-field-outline ng-tns-c92-236 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-236" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-236" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-236"></div></div><div class="mat-form-field-outline mat-form-field-outline-thick ng-tns-c92-236 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-236" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-236" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-236"></div></div><!----><!----><!----><div class="mat-form-field-infix ng-tns-c92-236"><!----><input _ngcontent-lkf-c122="" matinput="" class="mat-input-element mat-form-field-autofill-control ng-untouched ng-pristine ng-valid cdk-text-field-autofill-monitored ng-star-inserted" type="text" required="" id="partnerEmail" aria-invalid="false" aria-required="true" maxlength="100"><!----><span class="mat-form-field-label-wrapper ng-tns-c92-236"><label class="mat-form-field-label ng-tns-c92-236 ng-star-inserted" id="mat-form-field-label-385" for="partnerEmail" aria-owns="partnerEmail"><!----><mat-label _ngcontent-lkf-c122="" class="ng-tns-c92-236 ng-star-inserted"> Partner Email </mat-label><!----><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c92-236 ng-star-inserted"> *</span><!----></label><!----></span></div><!----></div><!----><div class="mat-form-field-subscript-wrapper ng-tns-c92-236"><!----><div class="mat-form-field-hint-wrapper ng-tns-c92-236 ng-trigger ng-trigger-transitionMessages ng-star-inserted" style="opacity: 1; transform: translateY(0%);"><!----><div class="mat-form-field-hint-spacer ng-tns-c92-236"></div></div><!----></div></div></mat-form-field></div><!----><div _ngcontent-lkf-c122=""><button _ngcontent-lkf-c122="" mat-icon-button="" aria-hidden="false" aria-label="add" class="mat-focus-indicator add-icon mat-icon-button mat-button-base"><span class="mat-button-wrapper"><mat-icon _ngcontent-lkf-c122="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true" data-mat-icon-type="font">add</mat-icon></span><span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span><span class="mat-button-focus-overlay"></span></button><button _ngcontent-lkf-c122="" mat-icon-button="" aria-hidden="false" aria-label="delete" class="mat-focus-indicator add-icon mat-icon-button mat-button-base" style="padding-left: 5px;"><span class="mat-button-wrapper"><mat-icon _ngcontent-lkf-c122="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true" data-mat-icon-type="font">delete</mat-icon></span><span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span><span class="mat-button-focus-overlay"></span></button></div></section></div><div _ngcontent-lkf-c122="" class="ng-untouched ng-pristine ng-valid ng-star-inserted"><section _ngcontent-lkf-c122="" class="resp-grid-partners"><div _ngcontent-lkf-c122="" class="ng-star-inserted"><mat-form-field _ngcontent-lkf-c122="" appearance="outline" class="mat-form-field form-field ng-tns-c92-238 mat-primary mat-form-field-type-mat-select mat-form-field-appearance-outline mat-form-field-can-float mat-form-field-has-label ng-untouched ng-pristine ng-valid mat-form-field-should-float"><div class="mat-form-field-wrapper ng-tns-c92-238"><div class="mat-form-field-flex ng-tns-c92-238"><div class="mat-form-field-outline ng-tns-c92-238 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-238" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-238" style="width: 74.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-238"></div></div><div class="mat-form-field-outline mat-form-field-outline-thick ng-tns-c92-238 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-238" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-238" style="width: 74.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-238"></div></div><!----><!----><!----><div class="mat-form-field-infix ng-tns-c92-238"><mat-select _ngcontent-lkf-c122="" role="combobox" aria-autocomplete="none" aria-haspopup="true" class="mat-select ng-tns-c95-241 mat-select-required ng-untouched ng-pristine ng-valid ng-star-inserted" aria-labelledby="mat-form-field-label-387 mat-select-value-45" id="mat-select-44" tabindex="0" aria-expanded="false" aria-required="true" aria-disabled="false" aria-invalid="false" required=""><div cdk-overlay-origin="" class="mat-select-trigger ng-tns-c95-241"><div class="mat-select-value ng-tns-c95-241" id="mat-select-value-45"><!----><span class="mat-select-value-text ng-tns-c95-241 ng-star-inserted"><span class="mat-select-min-line ng-tns-c95-241 ng-star-inserted">CHARTERER</span><!----><!----></span><!----></div><div class="mat-select-arrow-wrapper ng-tns-c95-241"><div class="mat-select-arrow ng-tns-c95-241"></div></div></div><!----></mat-select><!----><!----><span class="mat-form-field-label-wrapper ng-tns-c92-238"><label class="mat-form-field-label ng-tns-c92-238 ng-star-inserted" id="mat-form-field-label-387" for="mat-select-44" aria-owns="mat-select-44"><!----><mat-label _ngcontent-lkf-c122="" class="ng-tns-c92-238 ng-star-inserted"> Partner Role </mat-label><!----><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c92-238 ng-star-inserted"> *</span><!----></label><!----></span></div><!----></div><!----><div class="mat-form-field-subscript-wrapper ng-tns-c92-238"><!----><div class="mat-form-field-hint-wrapper ng-tns-c92-238 ng-trigger ng-trigger-transitionMessages ng-star-inserted" style="opacity: 1; transform: translateY(0%);"><!----><div class="mat-form-field-hint-spacer ng-tns-c92-238"></div></div><!----></div></div></mat-form-field></div><div _ngcontent-lkf-c122="" class="ng-star-inserted"><mat-form-field _ngcontent-lkf-c122="" appearance="outline" class="mat-form-field form-field ng-tns-c92-239 mat-primary mat-form-field-type-mat-input mat-form-field-appearance-outline mat-form-field-can-float mat-form-field-should-float mat-form-field-has-label ng-untouched ng-pristine ng-valid"><div class="mat-form-field-wrapper ng-tns-c92-239"><div class="mat-form-field-flex ng-tns-c92-239"><div class="mat-form-field-outline ng-tns-c92-239 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-239" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-239" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-239"></div></div><div class="mat-form-field-outline mat-form-field-outline-thick ng-tns-c92-239 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-239" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-239" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-239"></div></div><!----><!----><!----><div class="mat-form-field-infix ng-tns-c92-239"><!----><input _ngcontent-lkf-c122="" matinput="" class="mat-input-element mat-form-field-autofill-control ng-untouched ng-pristine ng-valid cdk-text-field-autofill-monitored ng-star-inserted" type="text" required="" id="partnerName" aria-invalid="false" aria-required="true" maxlength="100"><!----><span class="mat-form-field-label-wrapper ng-tns-c92-239"><label class="mat-form-field-label ng-tns-c92-239 ng-star-inserted" id="mat-form-field-label-389" for="partnerName" aria-owns="partnerName"><!----><mat-label _ngcontent-lkf-c122="" class="ng-tns-c92-239 ng-star-inserted"> Partner name </mat-label><!----><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c92-239 ng-star-inserted"> *</span><!----></label><!----></span></div><!----></div><!----><div class="mat-form-field-subscript-wrapper ng-tns-c92-239"><!----><div class="mat-form-field-hint-wrapper ng-tns-c92-239 ng-trigger ng-trigger-transitionMessages ng-star-inserted" style="opacity: 1; transform: translateY(0%);"><!----><div class="mat-form-field-hint-spacer ng-tns-c92-239"></div></div><!----></div></div></mat-form-field></div><div _ngcontent-lkf-c122="" class="ng-star-inserted"><mat-form-field _ngcontent-lkf-c122="" appearance="outline" class="mat-form-field form-field ng-tns-c92-240 mat-primary mat-form-field-type-mat-input mat-form-field-appearance-outline mat-form-field-can-float mat-form-field-should-float mat-form-field-has-label ng-untouched ng-pristine ng-valid"><div class="mat-form-field-wrapper ng-tns-c92-240"><div class="mat-form-field-flex ng-tns-c92-240"><div class="mat-form-field-outline ng-tns-c92-240 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-240" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-240" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-240"></div></div><div class="mat-form-field-outline mat-form-field-outline-thick ng-tns-c92-240 ng-star-inserted"><div class="mat-form-field-outline-start ng-tns-c92-240" style="width: 5.5px;"></div><div class="mat-form-field-outline-gap ng-tns-c92-240" style="width: 80.5px;"></div><div class="mat-form-field-outline-end ng-tns-c92-240"></div></div><!----><!----><!----><div class="mat-form-field-infix ng-tns-c92-240"><!----><input _ngcontent-lkf-c122="" matinput="" class="mat-input-element mat-form-field-autofill-control ng-untouched ng-pristine ng-valid cdk-text-field-autofill-monitored ng-star-inserted" type="text" required="" id="partnerEmail" aria-invalid="false" aria-required="true" maxlength="100"><!----><span class="mat-form-field-label-wrapper ng-tns-c92-240"><label class="mat-form-field-label ng-tns-c92-240 ng-star-inserted" id="mat-form-field-label-391" for="partnerEmail" aria-owns="partnerEmail"><!----><mat-label _ngcontent-lkf-c122="" class="ng-tns-c92-240 ng-star-inserted"> Partner Email </mat-label><!----><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c92-240 ng-star-inserted"> *</span><!----></label><!----></span></div><!----></div><!----><div class="mat-form-field-subscript-wrapper ng-tns-c92-240"><!----><div class="mat-form-field-hint-wrapper ng-tns-c92-240 ng-trigger ng-trigger-transitionMessages ng-star-inserted" style="opacity: 1; transform: translateY(0%);"><!----><div class="mat-form-field-hint-spacer ng-tns-c92-240"></div></div><!----></div></div></mat-form-field></div><!----><div _ngcontent-lkf-c122=""><button _ngcontent-lkf-c122="" mat-icon-button="" aria-hidden="false" aria-label="add" class="mat-focus-indicator add-icon mat-icon-button mat-button-base"><span class="mat-button-wrapper"><mat-icon _ngcontent-lkf-c122="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true" data-mat-icon-type="font">add</mat-icon></span><span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span><span class="mat-button-focus-overlay"></span></button><button _ngcontent-lkf-c122="" mat-icon-button="" aria-hidden="false" aria-label="delete" class="mat-focus-indicator add-icon mat-icon-button mat-button-base" style="padding-left: 5px;"><span class="mat-button-wrapper"><mat-icon _ngcontent-lkf-c122="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true" data-mat-icon-type="font">delete</mat-icon></span><span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span><span class="mat-button-focus-overlay"></span></button></div></section></div><!----></div>

Answer

Solution 1:

1.Get all table row, like you did:

List<WebElement> rows = driver.findElements(By.xpath("//div[contains(@class,'form-tab-name') and contains(normalize-space(),'Partners')]/following::div[contains(@class,'ng-untouched')]"));

2.Get the last row:

WebElement lastRow = rows.get(rows.size() - 1);

3.From last row, find the add button and click it

WebElement addBtn = lastRow.findElement(By.xpath(".//button[@aria-label='add']"));
addBtn.click();

Find element inside element example: https://stackoverflow.com/a/22242312/7574461


Solution 2: Get all add button, then click the last one.

List<WebElement> addBtns = driver.findElements(By.xpath("//button[@aria-label='add']"));
addBtns.get(addBtns.size() - 1).click();