我有一个包含项目和按钮列表的页面。每个项目的位置都是动态的,我要查找的项目可以是列表中的第1项,也可以是第100项。我需要按下的按钮会根据列表中的位置进行更改。并且列表中的每个项目的按钮名称都相同。我尝试根据名称进行选择,然后点击列表中的第一个按钮(我的那个时间是第11个)。我包括整行的HTML。我可以识别出一个部分,那就是QA GM 04012014 1424"一部分。
任何人都可以帮我点击正确的按钮吗? "开始新报价"是按钮名称。这是HTML:
<table class="controlLayout">
<tbody>
<tr id="section5_Row0" class="twControlTR">
<td class="sectionBodyCenterControl twControlTD " width="50%" valign="top" colspan="2" rowspan="1">
<input id="TBL_SELECTION_LIST_Table0" type="hidden" value="S^11" Name="TBL_SELECTION_LIST_tw#local#items">
<div id="Table0-div" class="repeatingTable">
<table id="Table0" class="tableControlNoLabel twControl twTable" cellspacing="0">
<tbody>
<tr id="_Header" class="tableControlHeader twTableHeaderTR">
<tr id="Table0_0" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_1" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_2" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_3" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_4" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_5" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_6" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_7" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_8" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_9" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_10" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_11" class="tableControlDataRow oddRow twTableTR selectedRow" style="display: table- row;">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<div>
<div id="CustomHTML3_11" class="customHTML">
QA GM 04012014 1424 Item 1 Name
<div style="display:none;">52448-QA GM 04012014 1424 Item 1 Description-QA GM 04012014 1424 Item 1 Name-Hardware-ELECTRICAL-CORDS</div>
</div>
</div>
</td>
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<div>
<div id="ButtonGroup4_11" class="buttonGroupNoLabel twControl twButtonGroup" align="right">
<button id="ButtonGroup4_Button0" class="layoutButton twButton ButtonGroup4_Button0" onclick="if (isFormSubmitted(this, true)) { return false; } else { return true; }" name="ButtonGroup4#Button0" type="submit">Start New Quote</button>
</div>
</div>
</td>
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
</tr>
<tr id="Table0_12" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_13" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_14" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_15" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_16" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_17" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_18" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_19" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_20" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_21" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_22" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_23" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_24" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_25" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_26" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_27" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_28" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_29" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_30" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_31" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_32" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_33" class="tableControlDataRow oddRow twTableTR" style="display: none;">
<tr id="Table0_34" class="tableControlDataRow evenRow twTableTR" style="display: none;">
<tr id="Table0_35" class="tableControlDataRow oddRow twTableTR" style="display: none;">
</tbody>
</table>
</div>
<script type="text/javascript">
</td>
</tr>
</tbody>
</table>
</div>
此处列出了特定行的HTML:
<tr id="Table0_11" class="tableControlDataRow oddRow twTableTR selectedRow" style="display: table- row;">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<div>
<div id="CustomHTML3_11" class="customHTML">
QA GM 04012014 1424 Item 1 Name
<div style="display:none;">52448-QA GM 04012014 1424 Item 1 Description-QA GM 04012014 1424 Item 1 Name-Hardware-ELECTRICAL-CORDS</div>
</div>
</div>
</td>
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
<div>
<div id="ButtonGroup4_11" class="buttonGroupNoLabel twControl twButtonGroup" align="right">
<button id="ButtonGroup4_Button0" class="layoutButton twButton ButtonGroup4_Button0" onclick="if (isFormSubmitted(this, true)) { return false; } else { return true; }" name="ButtonGroup4#Button0" type="submit">Start New Quote</button>
</div>
</div>
</td>
<td class="twTableTD" onclick="selectRow(this, 'single', false, 11, null, false);">
</tr>
答案 0 :(得分:0)
也许我过分简化了这一点,但如果Start New Quote
只在页面中出现一次,那么使用CSS查找按钮元素可以通过以下方式完成:
css = button:contains("Start New Quote")
如果有效,请告知我们。
修改强>
我正在考虑其他方法,但如果有任何机会,按钮的id
始终为ButtonGroup4_xx
,那么您可以使用以下XPath:
//button[starts-with(@id, 'ButtonGroup4_')]
如果有帮助,请告诉我们。
<强> EDIT2:强>
这似乎正在我的努力。当然,这很脏,但它确实有效。
//div[contains(., 'QA GM 04012014 1424')]/parent::div/parent::div/parent::td/following-sibling::td[8]/div/div/button
使用Selenium IDE和Python的lxml
库进行测试,并正确返回按钮。