I'm trying to add some text to a textbox in my html but the xpath cannot find the path and I don't know why.

this is my code: PropertiesCollections.driver.FindElement(By.XPath("//*[@id='from0']")).SendKeys("SS");

and this is the Html:

<div class="waypoint-container">
    <div class="float-left margin-right locationSelector" style="position: relative;z-index: 1">
        @Html.NgLabelFor(waypoint => waypoint.LocationLabel, new { @class = "block-display" })
        <input id="from{{$index}}" name="Location" index="{{$index}}" location-is-valid="locationIsValid" is-valid="locationIsValid" auto-complete-box get-validator-options="getLocations" on-selection="updateAutoCompleteSelection" get-auto-complete-options="locationSearch" class="airport-auto-complete waypoint-location-{{$index}}" ie-placeholder="true" ng-model="waypoint.Location" placeholder="Enter place or airport" required="" tabindex="{{getTabIndex($index,0)}}" type="text" ng-blur="onLocationChange($index)" ng-focus="onLocationFocus()"></input>
        <validation-message err-message="'Location is not valid'" is-show="locationIsValid!=null &&!locationIsValid && locationWasBlured" left="80px" top="24px" width="150px" flipped="'false'" />
    <div class="float-left margin-right" ng-show="waypoint.ShowDate">
        @Html.NgLabelFor(waypoint => waypoint.DateLabel, new { @class = "block-display" })
        <input ng-keydown="onDatePickerWayPointBlur($event)" autocomplete="off" type="text" class="begin-trip-date date-input datepicker-padding" name="Date" data_index="{{$index}}" placeholder="ddMMMyy" ng-model="waypoint.Date" tabindex="{{getTabIndex($index,2)}}" ie_placehoder="true" ui-date="waypoint.datePickerOptions" ui-date-time-format="{{DateFormat.datepicker}}" ng-required="waypoint.ShowDate" />
    <div class="float-left margin-right" ng-show="waypoint.ShowDate">
        <select ng-model="waypoint.SelectedDateType" ng-options="dateType as dateType.Text for dateType in waypoint.DateTypes"
                chosen="{width:'105px'}" disable-search="true" tabindex="100">
            <option value=""></option>

The problem may be partly that you are showing us the code used to generate the HTML, not the final HTML itself.

I say this becuase I see you have <input id="from{{$index}}" and that is obviously not the actual source of what you are trying to match. If this is the actual HTML then the problem is that your pre-parser isn't working or running. You should not be seeing the characters ($index) in your actual page.

In this case I suspect that what you are trying to target is probably from[0] instead of from0


As Michael Durrant said, never use the source code to generate your locators, use the DOM. Even better, Use the DOM but keep in mind how your source generates it and make sure you handle all (valid) alterations it could make.

In this example, you use an id that still has to be generated. That's not really optimal, once again because of the reasons Mr. Durrant pointed out. Yet there is a perfectly static selector available: ".waypoint-container .locationSelector input". Not only is a css selector preferable over an xpath but it usually works without needing to rely on dynamic values.

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.