2014-04-22 107 views
0

我對你有另一個動態問題。我有三個輸入字段,我需要輸入值。這些"dijit_form_DateTextBox_36"是動態創建的(36是可變的)。代碼中有一個值是獨一無二的,可以進行搜索。我知道我需要使用像這樣的東西:協助動態生成字段

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

但我仍然太新,這個東西能夠弄明白。如果有人能夠幫助我解決我需要解決的問題,以及他們如何解決問題,我將不勝感激。我真的很想自己解決這些問題!代碼如下。粗體部分是可以搜索的靜態文本。粗線和斜體線是需要修改的三個控件。注意:id="shipDate0_0"也是動態的,它可以是shipDate0_0,shipDate1_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> 
+0

我可以用我的方式一直工作到父行...... // b [contains(。,'QA GM 04012014 1424 Item Name')]/parent :: p/parent :: td/parent :: tr ...現在想出如何深入到我想要的項目。遇到困難。 – GregMa

+0

哇,你可能想把你的代碼片段修剪成只是有問題的元素,以及一些周圍的元素 –

+0

希望我已經將它修剪成只有該區域周圍的部分。 – GregMa

回答

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(),但在文本之前/之後修飾任何空格。&我發現它在不久之前發現它是非常寶貴的。文本()將返回空白,而且往往難以匹配

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

這下一部分前一個元素後找到的所有TD的。從提供的代碼段,這將發現所有13名TD的是通過查找僅包含你感興趣的ID的TD後續

/following-sibling::td

縮小範圍定爲13,這是優於使用硬編碼號碼等/following-sibling::td[2]找到第二TD

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

然後找到你感興趣的輸入字段中,有很多方法可以做到這一點,選擇你喜歡哪個

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

我希望這是對你不夠清楚&作品井數,請讓我知道如果不是

+0

工作正常!它比我想出的要乾淨得多。我想出了「driver.findElement(By.xpath(」// b [contains(。,'「+ prop.getProperty(」item.text「)+」Item Name「+」')]/parent :: p /父:: TD /父:: TR/TD [6]/DIV/DIV/DIV /跨度/ DIV/DIV [3] /輸入 「))明確();」。謝謝! – GregMa

+0

太棒了,很高興幫助:) –