我想将 //input[@id=(//label[text()='Search for:']/@for)]
转换为 selenium 中的 Css 选择器
以下是HTML
<td class="mstrSearchFieldSearchBox">
<div title="" class="mstrTextBoxWithIcon" id="id_mstr56" style="display: block;">
<div class="mstrTextBoxWithIconCaption">
<label for="id_mstr56_txt">Search for:</label>
</div>
<table cellspacing="0" cellpadding="0" class="mstrTextBoxWithIconTable">
<tbody>
<tr>
<td class="mstrTextBoxWithIconCellInput">
<div>
<input maxlength="" onclick="if (mstr.utils.ISW3C) {this.focus();}" onkeypress="return mstr.behaviors.TextBoxWithIcon.onkeypress(arguments[0], self, 'id_mstr56', this.value)" name="id_mstr56_txt" style="background-color: rgb(255, 255, 255);" id="id_mstr56_txt" size="" type="text">
</div>
</td>
<td class="mstrTextBoxWithIconCellIcon">
<div class="mstrToolButtonRoundedRight" style="background-position: left center;">
<input type="button" title="Search" onmouseout="mstr.behaviors.ToolButtonRounded.unhover(this.parentNode)" style="background-position: left center; cursor: pointer;" class="mstrBGIcon_tbSearch" src="../images/1ptrans.gif" onmouseover="mstr.behaviors.ToolButtonRounded.hover(this.parentNode)" onclick="mstr.behaviors.TextBoxWithIcon.fire('id_mstr56', this.parentNode.parentNode.previousSibling.childNodes[0].childNodes[0].value);return false;">
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
最佳答案
尝试使用 CSS 选择器“label[for="id_mstr56_txt"]”。只要只有一个标签的 for 属性为“id_mstr56_txt”,这就应该有效。
关于selenium - 我想将给定的 Xpath 转换为 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44070049/