英文:
How to check the checkbox in mat-row which is associated with a specific text using Selenium
问题
在嵌套的mat-row
中,您想要检查包含 AUTO_001 的行中的复选框。您尝试了下面的代码,但没有成功:
WebElement checkbox = setup.driver.findElement(By.xpath("//*[contains(text(),'AUTO_001')]/input[@class='mat-checkbox-input cdk-visually-hidden']"));
checkbox.click();
以下是成功检查复选框的代码:
// 通过包含文本 'AUTO_001' 的元素找到包含复选框的父元素
WebElement rowContainingAuto001 = setup.driver.findElement(By.xpath("//*[contains(text(),'AUTO_001')]"));
// 在包含 'AUTO_001' 的父元素中找到复选框
WebElement checkbox = rowContainingAuto001.findElement(By.cssSelector("input.mat-checkbox-input.cdk-visually-hidden"));
// 单击复选框来选中它
checkbox.click();
这段代码首先通过包含文本 'AUTO_001' 的元素找到包含复选框的父元素,然后在该父元素中找到复选框并单击它来选中它。
英文:
I am trying to check the checkbox in nested mat-row below:
<mat-row _ngcontent-dac-c252="" role="row" class="mat-row cdk-row ng-star-inserted">
<mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-select mat-column-select ng-star-inserted">
<mat-checkbox _ngcontent-dac-c252="" class="mat-checkbox mat-accent ng-valid ng-dirty ng-touched" id="mat-checkbox-123">
<label class="mat-checkbox-layout" for="mat-checkbox-123-input">
<span class="mat-checkbox-inner-container mat-checkbox-inner-container-no-side-margin">
<input type="checkbox" class="mat-checkbox-input cdk-visually-hidden" id="mat-checkbox-123-input" tabindex="0" aria-checked="false">
<span matripple="" class="mat-ripple mat-checkbox-ripple mat-focus-indicator">
<span class="mat-ripple-element mat-checkbox-persistent-ripple"></span>
</span>
<span class="mat-checkbox-frame"></span>
<span class="mat-checkbox-background">
<svg version="1.1" focusable="false" viewBox="0 0 24 24" xml:space="preserve" aria-hidden="true" class="mat-checkbox-checkmark">
<path fill="none" stroke="white" d="M4.1,12.7 9,17.6 20.3,6.3" class="mat-checkbox-checkmark-path"></path>
</svg>
<span class="mat-checkbox-mixedmark"></span>
</span>
</span>
<span class="mat-checkbox-label">
<span style="display: none;">&nbsp;</span>
</span>
</label>
</mat-checkbox>
</mat-cell>
<mat-cell _ngcontent-dac-c252="" role="cell" class="mat-tooltip-trigger mat-cell cdk-cell cdk-column-type mat-column-type ng-star-inserted" aria-describedby="cdk-describedby-message-34" cdk-describedby-host="0">
<span _ngcontent-dac-c252="" class="mobile-label">Type</span>
<button _ngcontent-dac-c252="" type="button" mat-icon-button="" class="mat-focus-indicator mat-icon-button mat-button-base mat-primary">
<span class="mat-button-wrapper">
<mat-icon _ngcontent-dac-c252="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true" data-mat-icon-type="font">folder_open</mat-icon>
<!---->
<!---->
<!---->
<!---->
</span>
<span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span>
<span class="mat-button-focus-overlay"></span>
</button>
</mat-cell>
<!---->
<mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-name mat-column-name ng-star-inserted">
<span _ngcontent-dac-c252="" class="mobile-label">Name </span>
<a _ngcontent-dac-c252="" style="cursor: pointer;" class="ng-star-inserted"> AUTO_001 </a>
<!---->
<!---->
</mat-cell>
<mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-updated mat-column-updated ng-star-inserted">
<span _ngcontent-dac-c252="" class="mobile-label">Date modified</span> 2/13/2023, 2:06:26 PM
</mat-cell>
<mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-edit mat-column-edit ng-star-inserted" hidden="">
<span _ngcontent-dac-c252="" class="mobile-label">Edit</span>
<button _ngcontent-dac-c252="" type="button" mat-icon-button="" aria-label="Edit" class="mat-focus-indicator mat-tooltip-trigger mat-icon-button mat-button-base">
<span class="mat-button-wrapper">
<mat-icon _ngcontent-dac-c252="" role="img" color="primary" class="mat-icon notranslate mat-primary material-icons" aria-hidden="true" data-mat-icon-type="font">edit</mat-icon>
</span>
<span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span>
<span class="mat-button-focus-overlay"></span>
</button>
<!---->
<!---->
<!---->
</mat-cell>
<!---->
</mat-row>
Deep down in this row, there is a checkbox and I would like to check the checkbox of the row contains AUTO_001.
I tried this but it didn't work:
WebElement checkbox = setup.driver.findElement(By.xpath("//*[contains(text(),'AUTO_001')]//input[@class='mat-checkbox-input cdk-visually-hidden']"));
checkbox.click();
Could you please assist how I can check the checkbox?
答案1
得分: 0
该元素是一个Angular元素,所以要对该元素进行click()操作,您需要使用WebDriverWait等待elementToBeClickable(),并且您可以使用以下定位策略:
- 使用_xpath_:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='ng-star-inserted' and contains(., 'AUTO_001')]//ancestor::mat-cell[1]//preceding-sibling::mat-cell[2]//input")).click();
更新
作为替代,您可以使用executeScript()
方法,如下所示:
WebElement elem = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='ng-star-inserted' and contains(., 'AUTO_001')]//ancestor::mat-cell[1]//preceding-sibling::mat-cell[2]//input"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", elem);
英文:
The element is an Angular element, so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use the following locator strategies:
-
Using xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='ng-star-inserted' and contains(., 'AUTO_001')]//ancestor::mat-cell[1]//preceding-sibling::mat-cell[2]//input"))).click();
Update
As an alternative you can use the executeScript()
method as follows:
WebElement elem = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='ng-star-inserted' and contains(., 'AUTO_001')]//ancestor::mat-cell[1]//preceding-sibling::mat-cell[2]//input")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", elem);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论