协助动态生成的字段

时间:2014-04-22 22:05:42

标签: selenium selenium-webdriver

我还有另一个动态问题。我有三个输入字段,我需要输入值。这些"dijit_form_DateTextBox_36"是动态创建的(36是可变的)。代码中较早的值是唯一的,可以搜索。我知道我需要使用类似的东西:

driver.findElement(By.xpath("//div[contains(., 'QA GM 04012014 1424 Item Name')]/parent::...somepath.sendKeys("...");

但我仍然对这些东西太新了,无法弄明白。如果有人可以帮助我完成我需要的东西,以及他们如何解决这个问题,我将非常感激。我真的很想自己解决这些问题!代码如下。粗体部分是可以搜索的静态文本。粗体和斜体线是需要修改的三个控件。注意:id="shipDate0_0"也是动态的,可以是shipDate0_0shipDate1_0等。所以无法关键。

    <tr class="tableControlHeader twTableHeaderTR"></tr>
    <tr class="tableControlDataRow evenRow twTableTR">
    <td class="twTableTD details" align="center" rowspan="2"></td>
    <td class="twTableTD details" align="center" rowspan="2">
        <p>
            <b>

                QA GM 04012014 1424 Item Name

            </b>
        </p>
        <br></br>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
    </td>
    <td class="twTableTD" align="center" rowspan="2"></td>
    <td class="twTableTD" align="center"></td>
    <td class="twTableTD" align="center"></td>
    <td id="shipDate0_0" class="twTableTD" align="center">
        <div style="padding-right: 20px;">
            <div id="dateWrap-projectedFirstShipDate_0_0" class="inputText_Full twControl twDateSelector" ;="" onblur="updateAvgPerWeek(0,0)" initialvalue="" value="" name="tw#local#quoteComparison#0#country#0#projectedFirstShipDate" style="white-space:nowrap;">
                <div id="projectedFirstShipDate_0_0" lang="" dojoattachpoint="pickerDiv" widgetid="projectedFirstShipDate_0_0" name="tw#local#quoteComparison#0#country#0#projectedFirstShipDate">
                    <span dojoattachpoint="leftPicker">
                        <div id="widget_dijit_form_DateTextBox_36" class="dijit dijitReset dijitInlineTable dijitLeft dateSelectionSin…tBox dijitComboBox dijitDateTextBox dijitComboBoxOpenOnClick" role="combobox" widgetid="dijit_form_DateTextBox_36">
                            <div class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" dojoattachpoint="_buttonNode, _popupStateNode" popupactive="true"></div>
                            <div class="dijitReset dijitValidationContainer"></div>
                            <div class="dijitReset dijitInputField dijitInputContainer">
                                <input id="dijit_form_DateTextBox_36" class="dijitReset dijitInputInner" type="text" aria-haspopup="true" role="textbox" dojoattachpoint="textbox,focusNode" autocomplete="off" aria-valuenow="null" aria-invalid="false" tabindex="0" value="" style="" delocalized="null" aria-disabled="false"></input>
                                <input type="hidden" value=""></input>
                            </div>
                        </div>
                    </span>
                    <span dojoattachpoint="rightPicker"></span>
                    <span style="position: absolute;" dojoattachpoint="calImage"></span>
                    <input id="projectedFirstShipDate_0_0" type="text" isdatefield="true" name="tw#local#quoteComparison#0#country#0#projectedFirstShipDate" style="display:none" dojoattachpoint="hiddenInput" delocalized="null"></input>
                </div>
            </div>
        </div>
    </td>
    <td id="InDate0_0" class="twTableTD" align="center" ;="" onclick="updateAvgPerWeek(0,0)">
        <div style="padding-right: 20px;">
            <div id="dateWrap-inDate_0_0" class="inputText_Full twControl twDateSelector" ;="" onblur="updateAvgPerWeek(0,0)" initialvalue="" value="" name="tw#local#quoteComparison#0#country#0#inDate" style="white-space:nowrap;">
                <div id="inDate_0_0" lang="" dojoattachpoint="pickerDiv" widgetid="inDate_0_0" name="tw#local#quoteComparison#0#country#0#inDate">
                    <span dojoattachpoint="leftPicker">
                        <div id="widget_dijit_form_DateTextBox_35" class="dijit dijitReset dijitInlineTable dijitLeft dateSelectionSin…tBox dijitComboBox dijitDateTextBox dijitComboBoxOpenOnClick" role="combobox" widgetid="dijit_form_DateTextBox_35">
                            <div class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" dojoattachpoint="_buttonNode, _popupStateNode" popupactive="true"></div>
                            <div class="dijitReset dijitValidationContainer"></div>
                            <div class="dijitReset dijitInputField dijitInputContainer">
                                <input id="dijit_form_DateTextBox_35" class="dijitReset dijitInputInner" type="text" aria-haspopup="true" role="textbox" dojoattachpoint="textbox,focusNode" autocomplete="off" aria-valuenow="null" aria-invalid="false" tabindex="0" value="" style="" delocalized="null" aria-disabled="false"></input>
                                <input type="hidden" value=""></input>
                            </div>
                        </div>
                    </span>
                    <span dojoattachpoint="rightPicker"></span>
                    <span style="position: absolute;" dojoattachpoint="calImage"></span>
                    <input id="inDate_0_0" type="text" isdatefield="true" name="tw#local#quoteComparison#0#country#0#inDate" style="display:none" dojoattachpoint="hiddenInput" delocalized="null"></input>
                </div>
            </div>
        </div>
    </td>
    <td id="OutDate0_0" class="twTableTD" align="center" onclick="updateAvgPerWeek(0,0)">
        <div style="padding-right: 20px;">
            <div id="dateWrap-outDate_0_0" class="inputText_Full twControl twDateSelector" ;="" onblur="updateAvgPerWeek(0,0)" initialvalue="" value="" name="tw#local#quoteComparison#0#country#0#outDate" style="white-space:nowrap;">
                <div id="outDate_0_0" lang="" dojoattachpoint="pickerDiv" widgetid="outDate_0_0" name="tw#local#quoteComparison#0#country#0#outDate">
                    <span dojoattachpoint="leftPicker">
                        <div id="widget_dijit_form_DateTextBox_34" class="dijit dijitReset dijitInlineTable dijitLeft dateSelectionSin…tBox dijitComboBox dijitDateTextBox dijitComboBoxOpenOnClick" role="combobox" widgetid="dijit_form_DateTextBox_34">
                            <div class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" dojoattachpoint="_buttonNode, _popupStateNode" popupactive="true"></div>
                            <div class="dijitReset dijitValidationContainer"></div>
                            <div class="dijitReset dijitInputField dijitInputContainer">
                                <input id="dijit_form_DateTextBox_34" class="dijitReset dijitInputInner" type="text" aria-haspopup="true" role="textbox" dojoattachpoint="textbox,focusNode" autocomplete="off" aria-valuenow="null" aria-invalid="false" tabindex="0" value="" style="" delocalized="null" aria-disabled="false"></input>
                                <input type="hidden" value=""></input>
                            </div>
                        </div>
                    </span>
                    <span dojoattachpoint="rightPicker"></span>
                    <span style="position: absolute;" dojoattachpoint="calImage"></span>
                    <input id="outDate_0_0" type="text" isdatefield="true" name="tw#local#quoteComparison#0#country#0#outDate" style="display:none" dojoattachpoint="hiddenInput" delocalized="null"></input>
                </div>
            </div>
        </div>
    </td>
    <td id="BuyQuantity0_0" class="twTableTD" align="center" onblur="updateAvgPerWeek(0,0)" name="BuyQuantity0"></td>
    <td id="TotalCost0_0" class="twTableTD" align="center" name="TotalCost0"></td>
    <td id="NumberOfWarehouses0_0" class="twTableTD" align="center" onblur="updateAvgPerWeek(0,0)" td=""></td>
    <!--

    # of Warehouses

    -->
    <td id="AveragePerWarehouse0_0" class="twTableTD" align="center" name="AvgPerWhouseWeek0"></td>
    <!--

    Cost per Warehouse

    -->`enter code here`
    <td id="ProjectedSellPrice0_0" class="twTableTD" align="center" td=""></td>
    <!--

     Projected Sell Price 

    -->
    <td id="PercentOfTotal0_0" class="twTableTD" align="center"></td>
    <td class="twTableTD" align="center" rowspan="2"></td>
</tr>

1 个答案:

答案 0 :(得分:0)

我能想出的最佳xpath是:

//td[normalize-space()='QA GM 04012014 1424 Item Name']/following-sibling::td[contains(@id, 'shipDate')]//input[contains(@class,'dijitInputInner')]

让我解释一下:

这部分找到带有你正在寻找的文本的td元素,normalize-space()调用就像text(),但修剪文本&amp;之前/之后的任何空格。我发现自从不久前发现它以来它是非常宝贵的。 text()将返回空格并且通常很难匹配

  

//td[normalize-space()='QA GM 04012014 1424 Item Name']

下一部分在前一个元素之后找到所有td。从提供的代码片段中可以找到以下所有13个td:

  

/following-sibling::td

通过仅查找包含您感兴趣的ID的td来缩小该组13,这比使用像/following-sibling::td[2]这样的硬编码查找第二个td更好

  

/following-sibling::td[contains(@id, 'shipDate')]

然后找到您感兴趣的输入字段,您可以通过多种方式执行此操作,选择您喜欢的方式

//input[contains(@class,'dijitInputInner')]
//input[contains(@id,'dijit_form_DateTextBox')]

我希望它足够清楚&amp;效果很好,如果没有,请告诉我