如何用Selenium找到这种元素

时间:2016-09-30 06:40:35

标签: python selenium

我试图学习如何使用selenium在python中创建脚本。大部分时间我都在练习"静态元素"但现在我想选择一些具有动态id的元素,但问题是他们的id没有部分静态部分。

然而他们有另一个&#34; id&#34;,这次是静态但我不知道如何在Python中使用它与selenium我已经尝试了两种不同的东西但是Selenium无法找到这个元素< / p>

这是元素的一部分:

<div class="table-cell show-plus" ng-class="{'show-plus': !user.deleted &amp;&amp; (authentication.user.isManager || user.isMe) &amp;&amp; (!group.isExchange || !ctrl.isPast(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="30308" data-is-exchange="false" data-shift-date="2016-09-29" id="5aa270ea-180f-2861-c97a-506c76cee386"><!----><div data-shift-id="134514" data-is-exchange="false" ng-repeat="event in ::(_events = (user.events[day] | filter:ctrl.filterOutMultiday | orderBy:['sort', 'dtstart'])) track by event.key" lvl-draggable="true" augment-draggable="" class="shift s12B835 published can-be-selected" ng-class="::{
              leave: event.type === 'leave',
              staging: event.status === 'planning',
              cancelled: event.cancelled,
              openend: event.isOpenEnded,
              published: event.status === 'published',
              unpublished: event.status !== 'published',
              unavailability: event.type === 'availability',
              'can-be-selected': event.canBeSelected
             }" draggable="true" id="c9d08710-daef-cdf7-9d7b-c4ceb13541a6"><!----><div class="shift-selection" ng-click="ctrl.stopEventPropagation($event)" ng-if="::event.canBeSelected"><div class="checkmark-wrapper"><label class="checkmark"><input type="checkbox" ng-model="ctrl.selectedEvents[event.id]" ng-change="ctrl.selectionChanged()" class="ng-pristine ng-untouched ng-valid ng-empty"> <span></span></label></div></div><!----><div class="shift-details" ng-click="ctrl.openShift(event, group.isExchange, $event)" ng-class="{'right-icon': event.showOfferedShiftIcon || event.showAvailableShiftIcon || event.showTimeoffIcon}"><div class="ellipsis" title="9:00A - 5:00P • 8H">9:00A - 5:00P • 8H <img style="margin-top:-2px" ng-show="event.tagId &amp;&amp; event.status !== 'planning'" src="/assets/svg/Icons/16/shift-repeat-white-8afc52.svg" class="ng-hide"> <img style="margin-top:-2px" ng-show="event.tagId &amp;&amp; event.status === 'planning'" src="/assets/svg/Icons/16/shift-repeat-gray-6d0e14.svg" class="ng-hide"></div><div class="ellipsis duration" ng-show="event.position">Design <span class="show-on-collapsed">• Carl Fairclough</span></div><div class="ellipsis duration ng-hide" ng-show="!event.position">Carl Fairclough</div><span class="shift-details-status ng-hide" ng-show="event.showOfferedShiftIcon"><img src="/assets/svg/Icons/24/shift-offer-white-cce33c.svg"> </span><span class="shift-details-status ng-hide" ng-show="event.showAvailableShiftIcon"><img src="/assets/svg/Icons/24/shift-available-white-c55aba.svg"> </span><span class="shift-details-status ng-hide" ng-show="event.showTimeoffIcon"><img src="/assets/svg/Icons/24/shift-timeoff-white-0cab28.svg"></span></div></div><!----></div>

<div data-shift-id="134514" data-is-exchange="false" ng-repeat="event in ::(_events = (user.events[day] | filter:ctrl.filterOutMultiday | orderBy:['sort', 'dtstart'])) track by event.key" lvl-draggable="true" augment-draggable="" class="shift s12B835 published can-be-selected" ng-class="::{
              leave: event.type === 'leave',
              staging: event.status === 'planning',
              cancelled: event.cancelled,
              openend: event.isOpenEnded,
              published: event.status === 'published',
              unpublished: event.status !== 'published',
              unavailability: event.type === 'availability',
              'can-be-selected': event.canBeSelected
             }" draggable="true" id="c9d08710-daef-cdf7-9d7b-c4ceb13541a6"><!----><div class="shift-selection" ng-click="ctrl.stopEventPropagation($event)" ng-if="::event.canBeSelected"><div class="checkmark-wrapper"><label class="checkmark"><input type="checkbox" ng-model="ctrl.selectedEvents[event.id]" ng-change="ctrl.selectionChanged()" class="ng-pristine ng-untouched ng-valid ng-empty"> <span></span></label></div></div><!----><div class="shift-details" ng-click="ctrl.openShift(event, group.isExchange, $event)" ng-class="{'right-icon': event.showOfferedShiftIcon || event.showAvailableShiftIcon || event.showTimeoffIcon}"><div class="ellipsis" title="9:00A - 5:00P • 8H">9:00A - 5:00P • 8H <img style="margin-top:-2px" ng-show="event.tagId &amp;&amp; event.status !== 'planning'" src="/assets/svg/Icons/16/shift-repeat-white-8afc52.svg" class="ng-hide"> <img style="margin-top:-2px" ng-show="event.tagId &amp;&amp; event.status === 'planning'" src="/assets/svg/Icons/16/shift-repeat-gray-6d0e14.svg" class="ng-hide"></div><div class="ellipsis duration" ng-show="event.position">Design <span class="show-on-collapsed">• Carl Fairclough</span></div><div class="ellipsis duration ng-hide" ng-show="!event.position">Carl Fairclough</div><span class="shift-details-status ng-hide" ng-show="event.showOfferedShiftIcon"><img src="/assets/svg/Icons/24/shift-offer-white-cce33c.svg"> </span><span class="shift-details-status ng-hide" ng-show="event.showAvailableShiftIcon"><img src="/assets/svg/Icons/24/shift-available-white-c55aba.svg"> </span><span class="shift-details-status ng-hide" ng-show="event.showTimeoffIcon"><img src="/assets/svg/Icons/24/shift-timeoff-white-0cab28.svg"></span></div></div>

我选择此元素的唯一方法是使用:

> data-shift-id="134514"

我尝试了与id和xpath不同的方法,但结尾的结果相同:&#34;无法找到这个元素&#34;

你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用以下属性构建cssSelector:

driver.find_element_by_css_selector("Yourtag[attributeName='Your AttributeValue']");

对于您的特定情况,请使用以下代码段:

driver.find_element_by_css_selector("div[data-shift-id='134514']");