如何查找具有动态ID的元素?我想选择id= select_option_32
,但通常会更改为select_option_60
或其他一些数字。有任何想法吗?
下面是XPATH和CSS选择器(以及脚本的屏幕截图)。
XPATH-
//*[@id="select_option_32"]
CSS选择器-
#select_option_32
下面是脚本:
下面是该类的完整元素:
<md-select-menu role="presentation" class="_md md-data-studio-theme md-overflow" style="transform-origin: 65.9653px 182.222px 0px;">
<md-content class="_md md-data-studio-theme">
<!-- ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_76" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Fixed
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_77" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last 7 days
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_78" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last 14 days
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_79" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last 28 days
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_80" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last 30 days
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_81" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Today
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_82" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Yesterday
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_83" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This week (starts Sunday)
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_84" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This week to date (starts Sunday)
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_85" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last week (starts Sunday)
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_86" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This week (starts Monday)
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_87" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This week to date (starts Monday)
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_88" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last week (starts Monday)
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_89" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This month
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_90" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This month to date
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_91" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last month
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_92" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This quarter
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_93" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This quarter to date
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_94" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last quarter
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_95" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
This year
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="true" id="select_option_96" aria-checked="true" value="[object Object]" selected="selected">
<div class="md-text ng-binding">
This year to date
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_97" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Last year
**
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_98" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Auto date range
</div>
</md-option>
<!-- end ngRepeat: option in** $ctrl.dateRangeOptions -->
<md-option ng-repeat="option in $ctrl.dateRangeOptions" ng-value="option" tabindex="0" class="ng-scope md-data-studio-theme md-ink-ripple" role="option" aria-selected="false" id="select_option_99" aria-checked="true" value="[object Object]">
<div class="md-text ng-binding">
Advanced
</div>
</md-option>
<!-- end ngRepeat: option in $ctrl.dateRangeOptions -->
</md-content>
</md-select-menu>
答案 0 :(得分:1)
使用CSS选择器通过部分匹配来定位元素:
driver.find_element_by_css_selector("md-option[id^='select_option']")
它将找到所有以select_option
开头的id的所有选项,因此在您的情况下,使其唯一选择select_option_32
特定的元素。首先获取列表中的所有元素,然后匹配
all_element = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "md-option[id^='select_option']")))
for element in all_element:
if 'This month' in element.text:
print("expected element is there")
# perform your action here which you suppose to do
break
另一种方法是建立索引,如果您认为匹配的文本不值得,则可以在xpath中使用索引来定位固定的索引元素
//md-option[starts-with(@id,'select_option_')][32]
如果您正在寻找一个已经被选择的元素,那么xpath将会是
//md-option[starts-with(@id,'select_option_')][@selected='selected']
答案 1 :(得分:0)
您可以尝试同时使用XPath和CSS的 功能。
例如:
starts-with
)://*[starts-with(@id,'select_option_')]
^=
):document.querySelector('*[id^="select_option_"]')
答案 2 :(得分:0)
因此,我相信您正在寻找“去年”网络元素。正确吗?
答案 3 :(得分:0)
您可以将部分ID与所选属性一起使用:
// * [包含(@id,'select_options_')并且包含(@selected,'selected')]
答案 4 :(得分:0)
您可以尝试使用以下任何XPath
以下内容将您定位到“自动”数据范围的父元素:
//md-content[@class='_md md-data-studio-theme']//child::md-option[23]
下面的内容将带您完全进入自动日期范围
//md-content[@class='_md md-data-studio-theme']//child::md-option[23]/div[contains(text(),'Auto date range')]