我有一个带行选择的Angular表:
<div class="ag-root-wrapper-body ag-layout-normal ag-focus-managed" ref="rootWrapperBody"><div class="ag-tab-guard ag-tab-guard-top" role="presentation" tabindex="0"></div>
<!--AG-GRID-COMP--><div class="ag-root ag-unselectable ag-layout-normal" role="grid" unselectable="on" ref="gridPanel" aria-rowcount="2" aria-colcount="4">
<!--AG-HEADER-ROOT--><div class="ag-header ag-focus-managed ag-pivot-off" role="presentation" ref="headerRoot" unselectable="on" style="height: 26px; min-height: 26px;">
<div class="ag-pinned-left-header ag-hidden" ref="ePinnedLeftHeader" role="presentation" style="width: 0px; max-width: 0px; min-width: 0px;"><div class="ag-header-row ag-header-row-column" role="row" aria-rowindex="1" style="top: 0px; height: 25px; width: 0px;"></div></div>
<div class="ag-header-viewport" ref="eHeaderViewport" role="presentation">
<div class="ag-header-container" ref="eHeaderContainer" role="rowgroup" style="width: 700px; transform: translateX(0px);"><div class="ag-header-row ag-header-row-column" role="row" aria-rowindex="1" style="top: 0px; height: 25px; width: 700px;"><div class="ag-header-cell ag-focus-managed ag-header-cell-sortable" role="columnheader" unselectable="on" tabindex="-1" aria-sort="none" aria-colindex="1" col-id="operationCodeName" style="width: 300px; left: 0px;">
<div ref="eResize" class="ag-header-cell-resize ag-hidden" role="presentation"></div>
<!--AG-CHECKBOX--><div role="presentation" ref="cbSelectAll" class="ag-header-select-all ag-labeled ag-label-align-right ag-checkbox ag-input-field ag-hidden">
<div ref="eLabel" class="ag-input-field-label ag-label ag-hidden ag-checkbox-label"></div>
<div ref="eWrapper" class="ag-wrapper ag-input-wrapper ag-checkbox-input-wrapper" role="presentation">
<input ref="eInput" class="ag-input-field-input ag-checkbox-input" type="checkbox" id="ag-242-input" tabindex="-1" aria-label="Press Space to toggle all rows selection (unchecked)">
</div>
</div>
<div class="ag-cell-label-container ag-header-cell-sorted-none">
<div ref="eLabel" class="ag-header-cell-label" role="presentation" unselectable="on">
<span ref="eText" class="ag-header-cell-text" unselectable="on">Operation Code</span>
<span ref="eFilter" class="ag-header-icon ag-header-label-icon ag-filter-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-filter" unselectable="on" role="presentation"></span></span>
<span ref="eSortOrder" class="ag-header-icon ag-header-label-icon ag-sort-order ag-hidden" aria-hidden="true"></span>
<span ref="eSortAsc" class="ag-header-icon ag-header-label-icon ag-sort-ascending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-up"></i></span>
<span ref="eSortDesc" class="ag-header-icon ag-header-label-icon ag-sort-descending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-down"></i></span>
<span ref="eSortNone" class="ag-header-icon ag-header-label-icon ag-sort-none-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-none" unselectable="on" role="presentation"></span></span>
</div>
</div></div><div class="ag-header-cell ag-focus-managed ag-header-cell-sortable" role="columnheader" unselectable="on" tabindex="-1" aria-sort="none" aria-colindex="2" col-id="accessorialExpectedAmount" style="width: 150px; left: 300px;">
<div ref="eResize" class="ag-header-cell-resize ag-hidden" role="presentation"></div>
<!--AG-CHECKBOX--><div role="presentation" ref="cbSelectAll" class="ag-header-select-all ag-labeled ag-label-align-right ag-checkbox ag-input-field ag-hidden">
<div ref="eLabel" class="ag-input-field-label ag-label ag-hidden ag-checkbox-label"></div>
<div ref="eWrapper" class="ag-wrapper ag-input-wrapper ag-checkbox-input-wrapper" role="presentation">
<input ref="eInput" class="ag-input-field-input ag-checkbox-input" type="checkbox" id="ag-245-input" tabindex="-1" aria-label="Press Space to toggle all rows selection (unchecked)">
</div>
</div>
<div class="ag-cell-label-container ag-header-cell-sorted-none">
<div ref="eLabel" class="ag-header-cell-label" role="presentation" unselectable="on">
<span ref="eText" class="ag-header-cell-text" unselectable="on">Expected</span>
<span ref="eFilter" class="ag-header-icon ag-header-label-icon ag-filter-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-filter" unselectable="on" role="presentation"></span></span>
<span ref="eSortOrder" class="ag-header-icon ag-header-label-icon ag-sort-order ag-hidden" aria-hidden="true"></span>
<span ref="eSortAsc" class="ag-header-icon ag-header-label-icon ag-sort-ascending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-up"></i></span>
<span ref="eSortDesc" class="ag-header-icon ag-header-label-icon ag-sort-descending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-down"></i></span>
<span ref="eSortNone" class="ag-header-icon ag-header-label-icon ag-sort-none-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-none" unselectable="on" role="presentation"></span></span>
</div>
</div></div><div class="ag-header-cell ag-focus-managed ag-header-cell-sortable" role="columnheader" unselectable="on" tabindex="-1" aria-sort="none" aria-colindex="3" col-id="accessorialActualAmount" style="width: 150px; left: 450px;">
<div ref="eResize" class="ag-header-cell-resize ag-hidden" role="presentation"></div>
<!--AG-CHECKBOX--><div role="presentation" ref="cbSelectAll" class="ag-header-select-all ag-labeled ag-label-align-right ag-checkbox ag-input-field ag-hidden">
<div ref="eLabel" class="ag-input-field-label ag-label ag-hidden ag-checkbox-label"></div>
<div ref="eWrapper" class="ag-wrapper ag-input-wrapper ag-checkbox-input-wrapper" role="presentation">
<input ref="eInput" class="ag-input-field-input ag-checkbox-input" type="checkbox" id="ag-248-input" tabindex="-1" aria-label="Press Space to toggle all rows selection (unchecked)">
</div>
</div>
<div class="ag-cell-label-container ag-header-cell-sorted-none">
<div ref="eLabel" class="ag-header-cell-label" role="presentation" unselectable="on">
<span ref="eText" class="ag-header-cell-text" unselectable="on">Actual</span>
<span ref="eFilter" class="ag-header-icon ag-header-label-icon ag-filter-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-filter" unselectable="on" role="presentation"></span></span>
<span ref="eSortOrder" class="ag-header-icon ag-header-label-icon ag-sort-order ag-hidden" aria-hidden="true"></span>
<span ref="eSortAsc" class="ag-header-icon ag-header-label-icon ag-sort-ascending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-up"></i></span>
<span ref="eSortDesc" class="ag-header-icon ag-header-label-icon ag-sort-descending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-down"></i></span>
<span ref="eSortNone" class="ag-header-icon ag-header-label-icon ag-sort-none-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-none" unselectable="on" role="presentation"></span></span>
</div>
</div></div><div class="ag-header-cell ag-focus-managed ag-header-cell-sortable" role="columnheader" unselectable="on" tabindex="-1" aria-sort="none" aria-colindex="4" col-id="accessorialExpectedUOMShortName" style="width: 100px; left: 600px;">
<div ref="eResize" class="ag-header-cell-resize ag-hidden" role="presentation"></div>
<!--AG-CHECKBOX--><div role="presentation" ref="cbSelectAll" class="ag-header-select-all ag-labeled ag-label-align-right ag-checkbox ag-input-field ag-hidden">
<div ref="eLabel" class="ag-input-field-label ag-label ag-hidden ag-checkbox-label"></div>
<div ref="eWrapper" class="ag-wrapper ag-input-wrapper ag-checkbox-input-wrapper" role="presentation">
<input ref="eInput" class="ag-input-field-input ag-checkbox-input" type="checkbox" id="ag-261-input" tabindex="-1" aria-label="Press Space to toggle all rows selection (unchecked)">
</div>
</div>
<div class="ag-cell-label-container ag-header-cell-sorted-none">
<div ref="eLabel" class="ag-header-cell-label" role="presentation" unselectable="on">
<span ref="eText" class="ag-header-cell-text" unselectable="on">UOM</span>
<span ref="eFilter" class="ag-header-icon ag-header-label-icon ag-filter-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-filter" unselectable="on" role="presentation"></span></span>
<span ref="eSortOrder" class="ag-header-icon ag-header-label-icon ag-sort-order ag-hidden" aria-hidden="true"></span>
<span ref="eSortAsc" class="ag-header-icon ag-header-label-icon ag-sort-ascending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-up"></i></span>
<span ref="eSortDesc" class="ag-header-icon ag-header-label-icon ag-sort-descending-icon ag-hidden" aria-hidden="true"><i class="fas fa-arrow-circle-down"></i></span>
<span ref="eSortNone" class="ag-header-icon ag-header-label-icon ag-sort-none-icon ag-hidden" aria-hidden="true"><span class="ag-icon ag-icon-none" unselectable="on" role="presentation"></span></span>
</div>
</div></div></div></div>
</div>
<div class="ag-pinned-right-header ag-hidden" ref="ePinnedRightHeader" role="presentation" style="width: 0px; max-width: 0px; min-width: 0px;"><div class="ag-header-row ag-header-row-column" role="row" aria-rowindex="1" style="top: 0px; height: 25px; width: 0px;"></div></div>
</div>
<div class="ag-floating-top" ref="eTop" role="presentation" unselectable="on" style="min-height: 0px; height: 0px; display: none; overflow-y: hidden;">
<div class="ag-pinned-left-floating-top ag-hidden" ref="eLeftTop" role="presentation" unselectable="on"></div>
<div class="ag-floating-top-viewport" ref="eTopViewport" role="presentation" unselectable="on">
<div class="ag-floating-top-container" ref="eTopContainer" role="presentation" unselectable="on" style="width: 700px; transform: translateX(0px);"></div>
</div>
<div class="ag-pinned-right-floating-top ag-hidden" ref="eRightTop" role="presentation" unselectable="on"></div>
<div class="ag-floating-top-full-width-container ag-hidden" ref="eTopFullWidthContainer" role="presentation" unselectable="on"></div>
</div>
<div class="ag-body-viewport ag-layout-normal ag-row-no-animation" ref="eBodyViewport" role="presentation">
<div class="ag-pinned-left-cols-container ag-hidden" ref="eLeftContainer" role="presentation" unselectable="on" style="height: 50px;"><div role="row" row-index="0" aria-rowindex="2" row-id="0" comp-id="253" class="ag-row ag-row-even ag-row-level-0 ag-row-position-absolute ag-row-first ag-row-last ag-row-focus ag-row-selected" aria-selected="true" style="height: 50px; transform: translateY(0px); " aria-label="Press SPACE to deselect this row."></div></div>
<div class="ag-center-cols-clipper" ref="eCenterColsClipper" role="presentation" unselectable="on" style="height: 50px;">
<div class="ag-center-cols-viewport" ref="eCenterViewport" role="presentation" style="height: calc(100% + 17px);">
<div class="ag-center-cols-container" ref="eCenterContainer" role="rowgroup" unselectable="on" style="width: 700px; height: 50px;"><div role="row" row-index="0" aria-rowindex="2" row-id="0" comp-id="253" class="ag-row ag-row-even ag-row-level-0 ag-row-position-absolute ag-row-first ag-row-last ag-row-focus ag-row-selected ag-row-not-inline-editing" aria-selected="true" style="height: 50px; transform: translateY(0px); " aria-label="Press SPACE to deselect this row."><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="4" comp-id="263" col-id="accessorialExpectedUOMShortName" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 600px; ">m</div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="1" comp-id="254" col-id="operationCodeName" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 300px; left: 0px; ">Accessorial Charge</div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="2" comp-id="255" col-id="accessorialExpectedAmount" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value ag-cell-focus" style="width: 150px; left: 300px; ">3.00000000</div><div tabindex="-1" id="ted-test" unselectable="on" role="gridcell" aria-colindex="3" comp-id="256" col-id="accessorialActualAmount" class="ag-cell ag-cell-auto-height ag-cell-value ag-cell-not-inline-editing" style="width: 150px; left: 450px; ">NaN</div></div></div>
</div>
</div>
<div class="ag-pinned-right-cols-container ag-hidden" ref="eRightContainer" role="presentation" unselectable="on" style="height: 50px;"><div role="row" row-index="0" aria-rowindex="2" row-id="0" comp-id="253" class="ag-row ag-row-even ag-row-level-0 ag-row-position-absolute ag-row-first ag-row-last ag-row-focus ag-row-selected" aria-selected="true" style="height: 50px; transform: translateY(0px); " aria-label="Press SPACE to deselect this row."></div></div>
<div class="ag-full-width-container" ref="eFullWidthContainer" role="presentation" unselectable="on" style="height: 50px;"></div>
</div>
<div class="ag-floating-bottom" ref="eBottom" role="presentation" unselectable="on" style="min-height: 0px; height: 0px; display: none; overflow-y: hidden;">
<div class="ag-pinned-left-floating-bottom ag-hidden" ref="eLeftBottom" role="presentation" unselectable="on"></div>
<div class="ag-floating-bottom-viewport" ref="eBottomViewport" role="presentation" unselectable="on">
<div class="ag-floating-bottom-container" ref="eBottomContainer" role="presentation" unselectable="on" style="width: 700px; transform: translateX(0px);"></div>
</div>
<div class="ag-pinned-right-floating-bottom ag-hidden" ref="eRightBottom" role="presentation" unselectable="on"></div>
<div class="ag-floating-bottom-full-width-container ag-hidden" ref="eBottomFullWidthContainer" role="presentation" unselectable="on"></div>
</div>
<div class="ag-body-horizontal-scroll" ref="eHorizontalScrollBody" aria-hidden="true" style="height: 17px; max-height: 17px; min-height: 17px;">
<div class="ag-horizontal-left-spacer ag-scroller-corner" ref="eHorizontalLeftSpacer" style="width: 0px; max-width: 0px; min-width: 0px;"></div>
<div class="ag-body-horizontal-scroll-viewport" ref="eBodyHorizontalScrollViewport" style="height: 17px; max-height: 17px; min-height: 17px;">
<div class="ag-body-horizontal-scroll-container" ref="eBodyHorizontalScrollContainer" style="width: 700px; height: 17px; max-height: 17px; min-height: 17px;"></div>
</div>
<div class="ag-horizontal-right-spacer ag-scroller-corner" ref="eHorizontalRightSpacer" style="width: 0px; max-width: 0px; min-width: 0px;"></div>
</div>
<!--AG-OVERLAY-WRAPPER--><div class="ag-overlay ag-hidden" aria-hidden="true" ref="overlayWrapper">
<div class="ag-overlay-panel">
<div class="ag-overlay-wrapper ag-layout-normal ag-overlay-no-rows-wrapper" ref="eOverlayWrapper"></div>
</div>
</div>
</div>
<div class="ag-tab-guard ag-tab-guard-bottom" role="presentation" tabindex="0"></div></div>
(参见我有Nan
值的输入字段(
当我双击Nan值时,我得到:
<div ref="eLabel" class="ag-input-field-label ag-label ag-hidden ag-text-field-label"></div>
<div ref="eWrapper" class="ag-wrapper ag-input-wrapper ag-text-field-input-wrapper" role="presentation">
<input ref="eInput" class="ag-input-field-input ag-text-field-input" type="text" id="ag-285-input" aria-label="Input Editor">
</div>
</div></div></div></div></div>
完整代码:https://pastebin.com/hNvj56Pz
我用这个代码双击表格行,并在输入字段中插入一个值:
WebElement element = driver.findElement(By.xpath("/......iv[3]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
Thread.sleep(3000);
// Insert into Accessorial Tasks screen Actual value
insertInputFieldByXPath(driver, "...../input",
"3");
protected void insertInputFieldByXPath(WebDriver driver, String input_id, String value){
WebDriverWait webDriverWait = new WebDriverWait(driver, 15);
WebElement webElement = webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(input_id)));
webElement.clear(); // First we delete the old value in case if this is a spinner
webElement.sendKeys(value);
}
当我双击Selenium时,表行变成了输入字段。但是当我尝试输入一些值时,却找不到id,因为id是动态生成的。
如何获取输入字段的父项并插入值?
你知道如何解决这个问题吗?
当dom更新时,您需要再次获取元素,我认为doubleClick((不会这样做。我会尝试单击两次,获得新的输入元素,然后使用send键发送文本。