2

I have a board where days of the week are the column and people's name are the rows, So I can use two parameters to find the cell of an employee for a certain day: data-user-id='30646 and data-shift-date='2016-10-15'

I have tried to do that in Python:

driver.find_element_by_xpath( "//div[@data-user-id='30646']" and "//div[@data-shift-date='2016-10-15']").click()

But If I do that, it only respects the last condition and so it will select the first cell on the 15th October 2016.

Do you have any ideas to find an element by using two class's attributes?

This is the HTML part for the cell I want to click on:

<div class="table-cell show-plus no-events" ng-class="{'show-plus': ctrl.shouldShowPlus(user, group, day), 'no-events': !_events.length, 'is-today': day === ctrl.todayString}" ng-repeat="day in ctrl.currentDays" ng-click="ctrl.addShift(user, day, false, group.grouping, group.isExchange, group.key)" lvl-drop-target="true" on-drop="ctrl.dropped(dragEl, dropEl, event, 'week')" data-user-id="30646" data-is-exchange="false" data-shift-date="2016-10-15" id="a9125bcc-ab64-e112-a5d1-ff33ade19968"><!----></div>

I can't use id="a9125bcc-ab64-e112-a5d1-ff33ade19968", it is dynamic and changes all the time when I load my browser.

2

Yes, if I understand you right.

You can use this xpath expression:

driver.find_element_by_xpath( "//div[@data-user-id='30646'][@data-shift-date='2016-10-15']").click()

Your question was asked the wrong way, you do "NOT" find an element by with two classes, you find it by using two class's attributes.

You can combine any number of class attributes in order to uniquely locate an element, the generic expression is:

driver.find_element_by_xpath("//elementType[@firstAttributeTypte = 'firstAttributeValue'][@secondAttributeType='secondAttributeValue'][@thirdAttributeType='thirdAttributeValue']....");
  • Thanks, it works! It also works if I do that: ` #driver.find_element_by_xpath( "//div[@data-user-id='30646']/.[@data-shift-date='2016-10-15']").click()` Is there a huge difference when I use /. ? – LeBrequin Oct 16 '16 at 3:32
  • ./ is not part of stand xpath expression. Why type two additional chars if you can get away with not typing them at all? :-P – Yu Zhang Oct 16 '16 at 3:33
1

To add to @Yu Zhang's solution, you may also use the 'and' operator in the xpath. So your statement would look like :

driver.find_element_by_xpath( "//div[@data-user-id='30646' and @data-shift-date='2016-10-15']").click()

Similarly, you may also use OR operator in cases where you would like to identify elements if one of the attribute matches. Eg:

driver.find_element_by_xpath( "//div[@data-user-id='30646' or @data-shift-date='2016-10-15']").click()

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.