0

我有一个带有下表的网页

Assessment name |       Task             |    Status

 Test A         | Practice               |  Not started

                | Assessment             |  Not started

Test B          | Practice               | Not started

                | Assessment             | Not started

还可以找到它的 html 代码。

<table class="gridRow" cellspacing="0" summary="For each assessment" style="border-collapse:collapse;">
    <tr>
        <th class="gridHeaderHide" scope="col">&nbsp;</th><th class="gridHeader" scope="col">Assessment name</th><th class="gridHeader" scope="col">Task</th><th class="gridHeader" scope="col">Status</th><th class="gridHeader" scope="col" abbr="Overall time you will need">Overall time you will need</th><th class="gridHeader" scope="col" abbr="Time limit">Time limit</th><th class="gridHeader" scope="col"><a tabindex="502" href="javascript:__doPostBack('ctl00$ErrorMessageContentPlaceHolder$gdvAssessmentHome','Sort$Deadline')">Deadline</a></th><th class="gridHeaderWhatUNeed" scope="col" abbr="What you need">What you need </th>
    </tr><tr class="gridRow" align="center">
        <td class="gridcolHide" rowspan="2">
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_imgCompleted" title="Completed" src="../Images/completed.gif" alt="Completed" style="border-width:0px;" />
                                </div>
                            </td><td class="gridcol" rowspan="2">
                                <span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_lblCandidateFriendlyName">Test A</span>
                            </td><td class="gridcol">

                                <input type="submit" name="ctl00$ErrorMessageContentPlaceHolder$gdvAssessmentHome$ctl02$PracticeTask" value="Practice" id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_PracticeTask" tabindex="503" class="linkButton" />
                            </td><td class="gridcol">Not started</td><td class="gridcol">
                                15&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_lblMins1" class="label">Minutes</span>
                            </td><td class="centeredImage">
                                <div style='display:block'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_PractiseImage" src="../Images/AH_Transparent_Icon.gif" alt=" " style="border-width:0px;" />
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_imgClock" title="Timer on" src="../Images/timer_on.gif" alt="Timer on" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_lblMins" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_imgTimerPlusOn" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_Label2" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_imgTimerOff" title="Timer off" src="../Images/Timer_plus.gif" alt="Timer off" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_Label1" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_imgTimerPlusOff" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_Label3" class="label">Minutes</span>
                                </div>
                            </td><td class="gridcol" rowspan="2">21/03/2013</td><td class="gridcolWhatUNeed" rowspan="2">
                                <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl02_imgMouse" title="Mouse" src="../Images/Mouse.gif" alt="Mouse" style="border-width:0px;" />
                            </td>
    </tr><tr class="gridRow" align="center">
        <td class="gridcol">

                                <input type="submit" name="ctl00$ErrorMessageContentPlaceHolder$gdvAssessmentHome$ctl03$PracticeTask" value="Take assessment" id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_PracticeTask" tabindex="504" class="linkButton" />
                            </td><td class="gridcol">Not started</td><td class="gridcol">
                                30&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_lblMins1" class="label">Minutes</span>
                            </td><td class="centeredImage">
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_PractiseImage" src="../Images/AH_Transparent_Icon.gif" alt=" " style="border-width:0px;" />
                                </div>
                                <div style='display:block'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_imgClock" title="Timer on" src="../Images/timer_on.gif" alt="Timer on" style="border-width:0px;" />
                                    19&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_lblMins" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_imgTimerPlusOn" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    19&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_Label2" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_imgTimerOff" title="Timer off" src="../Images/Timer_plus.gif" alt="Timer off" style="border-width:0px;" />
                                    19&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_Label1" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_imgTimerPlusOff" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    19&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl03_Label3" class="label">Minutes</span>
                                </div>
                            </td>
    </tr><tr class="gridRow" align="center">
        <td class="gridcolHide" rowspan="2">
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgCompleted" title="Completed" src="../Images/completed.gif" alt="Completed" style="border-width:0px;" />
                                </div>
                            </td><td class="gridcol" rowspan="2">
                                <span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_lblCandidateFriendlyName">Test B</span>
                            </td><td class="gridcol">

                                <input type="submit" name="ctl00$ErrorMessageContentPlaceHolder$gdvAssessmentHome$ctl04$PracticeTask" value="Practice" id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_PracticeTask" tabindex="505" class="linkButton" />
                            </td><td class="gridcol">Not started</td><td class="gridcol">
                                15&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_lblMins1" class="label">Minutes</span>
                            </td><td class="centeredImage">
                                <div style='display:block'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_PractiseImage" src="../Images/AH_Transparent_Icon.gif" alt=" " style="border-width:0px;" />
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgClock" title="Timer on" src="../Images/timer_on.gif" alt="Timer on" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_lblMins" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgTimerPlusOn" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_Label2" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgTimerOff" title="Timer off" src="../Images/Timer_plus.gif" alt="Timer off" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_Label1" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgTimerPlusOff" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    &nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_Label3" class="label">Minutes</span>
                                </div>
                            </td><td class="gridcol" rowspan="2">21/03/2013</td><td class="gridcolWhatUNeed" rowspan="2">
                                <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgMouse" title="Mouse" src="../Images/Mouse.gif" alt="Mouse" style="border-width:0px;" /><img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgCalculator" title="Calculator" src="../Images/Calculator.gif" alt="Calculator" style="border-width:0px;" /><img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_imgPaper" title="Pen and paper" src="../Images/pen_and_paper.gif" alt="Pen and paper" style="border-width:0px;" />
                            </td>
    </tr><tr class="gridRow" align="center">
        <td class="gridcol">

                                <input type="submit" name="ctl00$ErrorMessageContentPlaceHolder$gdvAssessmentHome$ctl05$PracticeTask" value="Take assessment" id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_PracticeTask" tabindex="506" class="linkButton" />
                            </td><td class="gridcol">Not started</td><td class="gridcol">
                                30&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_lblMins1" class="label">Minutes</span>
                            </td><td class="centeredImage">
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_PractiseImage" src="../Images/AH_Transparent_Icon.gif" alt=" " style="border-width:0px;" />
                                </div>
                                <div style='display:block'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_imgClock" title="Timer on" src="../Images/timer_on.gif" alt="Timer on" style="border-width:0px;" />
                                    25&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_lblMins" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_imgTimerPlusOn" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    25&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_Label2" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_imgTimerOff" title="Timer off" src="../Images/Timer_plus.gif" alt="Timer off" style="border-width:0px;" />
                                    25&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_Label1" class="label">Minutes</span>
                                </div>
                                <div style='display:none'>
                                    <img id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_imgTimerPlusOff" title="Extra time allowed" src="../Images/Timer_plus.gif" alt="Extra time allowed" style="border-width:0px;" />
                                    25&nbsp;&nbsp;<span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl05_Label3" class="label">Minutes</span>
                                </div>
                            </td>
    </tr><tr class="gridRow" align="center">

现在我必须 clcik 测试 B - 练习链接我使用以下 xpath 来单击它,因为链接是动态加载的

xpath=//*[contains(text(),'Test B')]/../../td[3]/input

这适用于特定语言,但是当我更改网页的显示语言(比如中文)时,它不起作用。

请帮助如何点击链接。注意:网页可能单独包含Test B行或同时包含Test A和Test B。(动态加载)我必须根据测试选择练习链接

如果需要更多信息,请告诉我

4

2 回答 2

0

我认为这里的问题是,当您将显示语言从英语更改为另一种语言时,“Test B”的值也会发生变化。您在 XPath 标识符中指定 //*[contains(text(),'Test B')] 以显式查找"Test B",当语言更改时,Selenium 无法找到此值。

查看您的 HTML 代码,我觉得您对所有标签都有唯一的 ID 属性,我认为更改语言不会改变。

尝试在 XPath 中使用“Id”属性而不是文本,例如,

XPath("//td[@id='ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_lblCandidateFriendlyName']")

</td><td class="gridcol" rowspan="2">
    <span id="ctl00_ErrorMessageContentPlaceHolder_gdvAssessmentHome_ctl04_lblCandidateFriendlyName">Test B</span>
</td><td class="gridcol">

由于您必须针对不同的语言选项运行测试,因此您需要在 Web 对象识别方面具有一些独特性。我认为使用 Id 将至少在所有语言选项的每个测试用例的基础上保证对象识别的唯一性。

您可以遵循的另一种方法稍微复杂一些,基于选择的语言,将“Test B”的值也转换为相同的语言,并将其用作 XPath 字符串中的变量值。

步骤将是, 1. 将唯一标识“Test B”转换为 unicode。2. 将 unicode 值转换为您选择的浏览器语言。3. 在 XPath 中使用上述值作为变量字符串,以编程方式更改。

最后一种方法是您可以将 Web 元素表值存储到 IList 并使用 IList 中的索引值编写测试脚本以供对象引用。考虑到动态对象的创建,您还必须为不同的测试用例编写不同的脚本。即使您更改了浏览器语言,如果您使用 IList 中的索引值进行脚本编写,它将是标准的。

于 2013-03-21T15:40:52.540 回答
0

这不是很明显吗?您正在使用文本来识别元素,如果语言更改,它将不起作用。文本因语言而异。

做这样的事情怎么样?

If testing in English String text = "Test B"

Else if testing in Chinese String text = "测试B";

 xpath="//*[contains(text(), "+text+" )]/../../td[3]/input"

除了这种方法,您为什么不避免使用文本来识别元素,这样您就不再依赖于每次更改语言时需要传递的文本。或找出处理此动态内容的逻辑,您需要遵循手动测试此测试用例时执行的相同过程。

于 2013-03-22T19:45:05.910 回答