klitz1967
klitz1967

Reputation: 255

How do I get Python and Selenium to click an angularjs link by matching the link to user input text?

The link I would like to click is input by the user. For example the user inputs for variable track, "Tampa Bay Downs"

My python selenium test program will search the code below:

<a ng-click="updateFavorite()(raceInfo.trackAbbr); $event.stopPropagation();">EV
  i class="my-icon-favorite-fill my-icon-favorite-fill_active" ng-class="{
  'my-icon-favorite-fill' : !showActionFeedback, ...onFeedback, 'rotating spinner' :
  showActionFeedback}" update-favorite-action-feedback="" qa-label="race-favorite-icon">
    ::before
   </i>
</a>
<span class="ng-binding" ng-bind="::raceInfo.trackName" qa-label="race-track-name">
Tampa Bay Downs</span>
</td>

The successful code will find the input words "Tampa Bay Downs" and then click it to activate an angularjs action that would cause a separate page to open, which is the desired action.

I have tried to find the link using the correct xpath with no success.
I have also tried this with no success:

try:
    element = driver.find_element_by_css_selector("a[qa-label="+track+"]")
except:
    print "error finding css selector for track"

Upvotes: 1

Views: 850

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You can select using an xpath using preceding-sibling to find the a before the span tag that contains the track name i.e Tampa Bay Downs:

"//span[contains(.,'{}')]/preceding-sibling::a".format(track) 

Or more specifically using the span class name:

 "//span[@qa-label='race-track-name' and contains(.,'{}')]/preceding-sibling::a".format(track) 

Upvotes: 1

Related Questions