如何使用Selenium选中与特定文本关联的mat-row中的复选框。

huangapple go评论105阅读模式
英文:

How to check the checkbox in mat-row which is associated with a specific text using Selenium

问题

在嵌套的mat-row中,您想要检查包含 AUTO_001 的行中的复选框。您尝试了下面的代码,但没有成功:

  1. WebElement checkbox = setup.driver.findElement(By.xpath("//*[contains(text(),'AUTO_001')]/input[@class='mat-checkbox-input cdk-visually-hidden']"));
  2. checkbox.click();

以下是成功检查复选框的代码:

  1. // 通过包含文本 'AUTO_001' 的元素找到包含复选框的父元素
  2. WebElement rowContainingAuto001 = setup.driver.findElement(By.xpath("//*[contains(text(),'AUTO_001')]"));
  3. // 在包含 'AUTO_001' 的父元素中找到复选框
  4. WebElement checkbox = rowContainingAuto001.findElement(By.cssSelector("input.mat-checkbox-input.cdk-visually-hidden"));
  5. // 单击复选框来选中它
  6. checkbox.click();

这段代码首先通过包含文本 'AUTO_001' 的元素找到包含复选框的父元素,然后在该父元素中找到复选框并单击它来选中它。

英文:

I am trying to check the checkbox in nested mat-row below:

  1. <mat-row _ngcontent-dac-c252="" role="row" class="mat-row cdk-row ng-star-inserted">
  2. <mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-select mat-column-select ng-star-inserted">
  3. <mat-checkbox _ngcontent-dac-c252="" class="mat-checkbox mat-accent ng-valid ng-dirty ng-touched" id="mat-checkbox-123">
  4. <label class="mat-checkbox-layout" for="mat-checkbox-123-input">
  5. <span class="mat-checkbox-inner-container mat-checkbox-inner-container-no-side-margin">
  6. <input type="checkbox" class="mat-checkbox-input cdk-visually-hidden" id="mat-checkbox-123-input" tabindex="0" aria-checked="false">
  7. <span matripple="" class="mat-ripple mat-checkbox-ripple mat-focus-indicator">
  8. <span class="mat-ripple-element mat-checkbox-persistent-ripple"></span>
  9. </span>
  10. <span class="mat-checkbox-frame"></span>
  11. <span class="mat-checkbox-background">
  12. <svg version="1.1" focusable="false" viewBox="0 0 24 24" xml:space="preserve" aria-hidden="true" class="mat-checkbox-checkmark">
  13. <path fill="none" stroke="white" d="M4.1,12.7 9,17.6 20.3,6.3" class="mat-checkbox-checkmark-path"></path>
  14. </svg>
  15. <span class="mat-checkbox-mixedmark"></span>
  16. </span>
  17. </span>
  18. <span class="mat-checkbox-label">
  19. <span style="display: none;"> </span>
  20. </span>
  21. </label>
  22. </mat-checkbox>
  23. </mat-cell>
  24. <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">
  25. <span _ngcontent-dac-c252="" class="mobile-label">Type</span>
  26. <button _ngcontent-dac-c252="" type="button" mat-icon-button="" class="mat-focus-indicator mat-icon-button mat-button-base mat-primary">
  27. <span class="mat-button-wrapper">
  28. <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>
  29. <!---->
  30. <!---->
  31. <!---->
  32. <!---->
  33. </span>
  34. <span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span>
  35. <span class="mat-button-focus-overlay"></span>
  36. </button>
  37. </mat-cell>
  38. <!---->
  39. <mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-name mat-column-name ng-star-inserted">
  40. <span _ngcontent-dac-c252="" class="mobile-label">Name </span>
  41. <a _ngcontent-dac-c252="" style="cursor: pointer;" class="ng-star-inserted"> AUTO_001 </a>
  42. <!---->
  43. <!---->
  44. </mat-cell>
  45. <mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-updated mat-column-updated ng-star-inserted">
  46. <span _ngcontent-dac-c252="" class="mobile-label">Date modified</span> 2/13/2023, 2:06:26PM
  47. </mat-cell>
  48. <mat-cell _ngcontent-dac-c252="" role="cell" class="mat-cell cdk-cell cdk-column-edit mat-column-edit ng-star-inserted" hidden="">
  49. <span _ngcontent-dac-c252="" class="mobile-label">Edit</span>
  50. <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">
  51. <span class="mat-button-wrapper">
  52. <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>
  53. </span>
  54. <span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span>
  55. <span class="mat-button-focus-overlay"></span>
  56. </button>
  57. <!---->
  58. <!---->
  59. <!---->
  60. </mat-cell>
  61. <!---->
  62. </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:

  1. WebElement checkbox = setup.driver.findElement(By.xpath("//*[contains(text(),'AUTO_001')]//input[@class='mat-checkbox-input cdk-visually-hidden']"));
  2. checkbox.click();

Could you please assist how I can check the checkbox?

答案1

得分: 0

该元素是一个Angular元素,所以要对该元素进行click()操作,您需要使用WebDriverWait等待elementToBeClickable(),并且您可以使用以下定位策略

  • 使用_xpath_:
  1. 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()方法,如下所示:

  1. 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"));
  2. ((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:

    1. 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:

  1. 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")));
  2. ((JavascriptExecutor)driver).executeScript("arguments[0].click();", elem);

huangapple
  • 本文由 发表于 2023年2月14日 03:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定