1

I have the below script where I have taken the list of WebElement of the number of rows and subtracted with and then click on the Add button. But the problem over here is - script clicking on the Add button of the second row every time.

Below is my code:

    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();
                    System.out.println("no of rows:"+rowcount);
                    WebElement lastrow = noofrows.get(noofrows.size()-1);
                    DriverUtil.waitTime(1000);
                    WebElement addbtn = lastrow.findElement(By.xpath("//mat-icon[contains(normalize-space(),'add')]"));
                    
                    addbtn.click();

How to click on the Last row add button?

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 class="mat-form-field-outline-gap ng-tns-c92-234" style="width: 74.5px;"/>
                                <div class="mat-form-field-outline-end ng-tns-c92-234"/>
                            </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 class="mat-form-field-outline-gap ng-tns-c92-234" style="width: 74.5px;"/>
                                <div class="mat-form-field-outline-end ng-tns-c92-234"/>
                            </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>
                                    <!---->
                                </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>
                </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 class="mat-form-field-outline-gap ng-tns-c92-235" style="width: 80.5px;"/>
                                <div class="mat-form-field-outline-end ng-tns-c92-235"/>
                            </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 class="mat-form-field-outline-gap ng-tns-c92-235" style="width: 80.5px;"/>
                                <div class="mat-form-field-outline-end ng-tns-c92-235"/>
                            </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>
                    </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 class="mat-form-field-outline-gap ng-tns-c92-236" style="width: 80.5px;"/>
                                    <div class="mat-form-field-outline-end ng-tns-c92-236"/>
                                </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 class="mat-form-field-outline-gap ng-tns-c92-236" style="width: 80.5px;"/>
                                    <div class="mat-form-field-outline-end ng-tns-c92-236"/>
                                </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>
                        </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 class="mat-button-focus-overlay"/>
                        </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 class="mat-button-focus-overlay"/>
                        </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 class="mat-form-field-outline-gap ng-tns-c92-238" style="width: 74.5px;"/>
                                        <div class="mat-form-field-outline-end ng-tns-c92-238"/>
                                    </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 class="mat-form-field-outline-gap ng-tns-c92-238" style="width: 74.5px;"/>
                                        <div class="mat-form-field-outline-end ng-tns-c92-238"/>
                                    </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>
                                            <!---->
                                        </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>
                        </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 class="mat-form-field-outline-gap ng-tns-c92-239" style="width: 80.5px;"/>
                                        <div class="mat-form-field-outline-end ng-tns-c92-239"/>
                                    </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 class="mat-form-field-outline-gap ng-tns-c92-239" style="width: 80.5px;"/>
                                        <div class="mat-form-field-outline-end ng-tns-c92-239"/>
                                    </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>
                            </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 class="mat-form-field-outline-gap ng-tns-c92-240" style="width: 80.5px;"/>
                                            <div class="mat-form-field-outline-end ng-tns-c92-240"/>
                                        </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 class="mat-form-field-outline-gap ng-tns-c92-240" style="width: 80.5px;"/>
                                            <div class="mat-form-field-outline-end ng-tns-c92-240"/>
                                        </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>
                                </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 class="mat-button-focus-overlay"/>
                                </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 class="mat-button-focus-overlay"/>
                                </button>
                            </div>
                        </section>
                    </div>
                    <!---->
                </div>
0

You might wanna take a look at the last-of-type css selector property (https://developer.mozilla.org/de/docs/Web/CSS/:last-of-type). That way, instead of having the overhead of finding every element, labeling them for your selenium hub, allocating space for all the elements in the RAM and then throwing them away you could do a simple driver.findElement(By.cssSelector("button.mat-icon-button:last-of-type")) and get only the element you want to click on.

0

What, @Daniel said would be less lines of code but you can start with an array of elements

WebElement foo[] = driver.findElements(...);

Then get the last element in that array

WebElement bar = foo.get(foo.size-1);
0

Try to find a unique parent and then find the button. If thats not possible , a quick approach is to use last() method

(//mat-icon[contains(normalize-space(),'add')])[last()]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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