Venkat Polisetti
Venkat Polisetti

Reputation: 23

Python - Selenium: How to find input element with a nested label inside as span tag via XPATH

I am trying to find the input element inside a div tag with a sibling span with label containing text "Destination".

I am using Python with Selenium and looking for a XPATH expression. Tried various variations of XPATH expressions but could not find a solution.

<div class="mat-form-field-infix ng-tns-c94-19">
    <input _ngcontent-luj-c437="" matinput="" type="text" formcontrolname="locationInput" class="mat-input-element mat-form-field-autofill-control mat-autocomplete-trigger ng-tns-c94-19 cdk-text-field-autofill-monitored ng-touched ng-valid ng-dirty" id="mat-input-2" aria-invalid="false" aria-required="false" autocomplete="off" role="combobox" aria-autocomplete="list" aria-expanded="true" aria-haspopup="true" aria-activedescendant="mat-option-286" aria-owns="mat-autocomplete-2">
    <!---->
    <mat-autocomplete _ngcontent-luj-c437="" autoactivefirstoption="" class="ng-tns-c94-19"><!----></mat-autocomplete>
    <mat-menu _ngcontent-luj-c437="" class=""><!----></mat-menu>
    <span class="mat-form-field-label-wrapper ng-tns-c94-19">
        <label class="mat-form-field-label ng-tns-c94-19 ng-star-inserted" id="mat-form-field-label-7" for="mat-input-2" aria-owns="mat-input-2">
            <!---->
            <mat-label _ngcontent-luj-c437="" class="ng-tns-c94-19 ng-star-inserted">Destination</mat-label>
            <!---->
            <!---->
        </label>
        <!---->
    </span>
</div>

Thank you.

Upvotes: 1

Views: 118

Answers (1)

Vova
Vova

Reputation: 3547

Try to use something like this:

xpath_by_str = "//mat-label[text()='Destination']/ancestor::div[1]/input"

xpath_by_id = "//label[@id='mat-form-field-label-7']/ancestor::div[1]/input"

Upvotes: 2

Related Questions