我想将给定的Xpath转换为CSS

时间:2017-05-19 12:23:38

标签: 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>

3 个答案:

答案 0 :(得分:1)

尝试使用CSS选择器'label [for =“id_mstr56_txt”]'。只要只有一个标签的for属性为“id_mstr56_txt”,这应该可行。

答案 1 :(得分:0)

我希望您尝试使用tagName“input”标识元素,使用“search”标识标题。我们可以通过css识别元素,如下所示,

 "input[type='button'][title='search']"

此外,我们在标记名“input”下没有任何“id”属性。你的xpath是如何工作的?

答案 2 :(得分:-1)

你无法进行直接翻译,因为CSS选择器无法通过包含的文本定位元素,只有XPath才能这样做。可能有另一种方式,但很难说这是否在页面上是唯一的。

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table 
As
Return (  
    Select RetSeq = Row_Number() over (Order By (Select null))
          ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
    From  (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
    Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')