[英]Select first table row by default
我有这个 Angular 应用程序,它显示选定的表格行:
<div class="ag-center-cols-container" ref="eCenterContainer" role="rowgroup" unselectable="on" style="width: 432px; height: 100px;">
<div role="row" row-index="0" aria-rowindex="2" row-id="0" comp-id="370" class="ag-row ag-row-focus ag-row-even ag-row-selected ag-row-level-0 ag-row-position-absolute ag-row-first" 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="1" comp-id="371" col-id="rowCheckbox" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 32px; left: 0px; ">
<mobileweb-checkbox-selection-renderer _nghost-tkr-c21="" class="ng-star-inserted"><i _ngcontent-tkr-c21="" class="far fa-check-square"></i><i _ngcontent-tkr-c21="" class="far fa-square" hidden=""></i></mobileweb-checkbox-selection-renderer>
</div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="2" comp-id="372" col-id="lookupCode" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 32px;">4</div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="3" comp-id="373" col-id="shipmentLookupCode" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-focus ag-cell-value" style="width: 100px; left: 132px;">20</div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="4" comp-id="374" col-id="carrierName" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 232px;"></div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="5" comp-id="375" col-id="shipmentExpectedDate" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 332px;">2021-01-28T16:20:35.987</div>
</div>
<div role="row" row-index="1" aria-rowindex="3" row-id="1" comp-id="376" class="ag-row ag-row-no-focus ag-row-odd ag-row-level-0 ag-row-position-absolute ag-row-last" aria-selected="false" style="height: 50px; transform: translateY(50px); " aria-label="Press SPACE to select this row.">
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="1" comp-id="377" col-id="rowCheckbox" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 32px; left: 0px; ">
<mobileweb-checkbox-selection-renderer _nghost-tkr-c21="" class="ng-star-inserted"><i _ngcontent-tkr-c21="" class="far fa-check-square" hidden=""></i><i _ngcontent-tkr-c21="" class="far fa-square"></i></mobileweb-checkbox-selection-renderer>
</div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="2" comp-id="378" col-id="lookupCode" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 32px;">3</div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="3" comp-id="379" col-id="shipmentLookupCode" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 132px;">19</div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="4" comp-id="380" col-id="carrierName" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 232px;"></div>
<div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="5" comp-id="381" col-id="shipmentExpectedDate" class="ag-cell ag-cell-not-inline-editing ag-cell-auto-height ag-cell-value" style="width: 100px; left: 332px;">2021-01-28T12:36:31.057</div>
</div>
</div>
完整代码https://pastebin.com/GUaHHnpA
我如何始终单击第一行并使用带有 Java 的 Selenium WebDriver 选择它?
您为完整HTML code
共享的链接中没有<body> tag
,但它有</body>
,所以您能先re-verify
一下吗?
现在,根据上面的 HTML 代码,如果目标是选择第一个表格行,这是我的建议/推荐。
您可以通过两种方式实现这一目标:-
使用xpath indexing
Selenium - findElements()
使用Selenium - findElements()
方法
案例1的演示:
如果这个xpath
//div[@role='row']
代表HTML
行,你会看到很多进入 DOM。
在你的情况下,因为你想抓住第一项,它会是这样的:
(//div[@role='row'])[1]
如果您想选择2nd row
或3rd row
,我们可以通过从 xpath 上方更改此值来完成它:
[1]
到[2]
或[3]
在代码中,您可以像下面这样使用它:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement firstRow = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@role='row'])[1]")));
firstRow.click();
案例2的演示:
2.1 :
List<WebElement> allRows = driver.findElements(By.xpath("//div[@role='row']"));
WebElement firstRow = allRows.get(0);
firstRow.click();
要么
2.2 :
List<WebElement> allRows = driver.findElements(By.xpath("//div[@role='row']"));
for (WebElement rows : allRows) {
rows.click();
break;
}
以下部分是OP的要求(另外,它与原始问题无关)
如果您已经实现了WebDriverEventListener
接口或扩展的AbstractWebDriverEventListener Class
,则可以使用以下方法:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement firstRow = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@role='row'])[1]")));
beforeClickOn(firstRow, driver);
firstRow.click();
afterClickOn(firstRow, driver);
假设这是在class A
编写的,那么您可以像这样扩展它:
public class A extends AbstractWebDriverEventListener
第一行有ag-row-first
和ag-row-selected
类,您可以使用任何一个。 这将匹配 3 个元素(根据您在链接中发布的 html),因此您可以将其与没有ag-hidden
类的父元素组合
driver.findElement(By.cssSelector("div:not(.ag-hidden) > .ag-row-first"));
一般来说, findElement
会返回 DOM 中第一个匹配的元素,所以
driver.findElement(By.className(".ag-row-selected"));
或者它的等价物
driver.findElements(By.className(".ag-row-selected")).get(0);
如果该行确实是 DOM 中的第一行,则应该有效。
使用 Selenium 插件,您可以通过 XPath 或 CSS 表达式查找任何元素。
为了使用 CSS 表达式找到第一行,请使用以下字符串:
String cssExpresison = "div[role='row'][row-id='0']";
WebElement we = driver.findElement(By.cssSelector(cssExpression));
we.click();
您可以通过 XPath 使用第一行中写入的文本:
String xPathExpression = "//div[contains(text(), 'Press SPACE to deselect this row')]";
WebElement we = driver.findElement(By.xpath(xPathExpression));
we.click();
如果 we.click() 不起作用,请执行以下操作:
Actions builder = new Actions(driver);
builder.moveToElement(we).click().build().perform();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.