selenium - 我想将给定的 Xpath 转换为 CSS

标签 selenium testing selenium-webdriver automated-tests

我想将 //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/

相关文章:

c# - CSS 选择器 - 获取了解元素子元素的元素

python - 从父类继承单元测试

java - 如何在 MockMvc 中指定@RequestMapping 参数

ruby-on-rails - 我在尝试运行 cucumber 测试时遇到错误,该测试使用 selenium-webdriver 操作确认对话框

python - 如何从 Selenium 处理 Shadow DOM 中的元素

java - 如何使用 Selenium WebDriver 从下拉框中选择一个项目?

python-3.x - 如何在 docker 容器中使用 selenium 设置 python 应用程序

angular - 当 Cypress E2E 中的任何一个测试失败时,Azure 构建就会失败

java - 如何在 Selenium 中没有物理键盘的情况下按键盘键

python - Selenium 快速获取元素