【问题标题】:How to locate "::after" element in selenium locator?如何在硒定位器中定位“::after”元素?
【发布时间】:2020-02-03 07:14:11
【问题描述】:

我的页面上的复选框很少,默认情况下很少选中。 通过我的代码,我需要处理取消选中页面上的所有文本框,然后继续。

问题在于复选框(选中和未选中)具有完全相同的 HTML / DOM 结构,具有相同的属性/值,除了选中复选框时出现的“::after”。 如何编写定位器来确定元素是选中还是未选中,然后继续取消选中它。

<!-- When checkbox is unchecked -->
<label class="someclasslabel" on-click="[[event]]">
    <span>Male</span>
    <input type="checkbox" data-bind="checked: $properties.value">
    <span class="checkbox"></span>
</label>


<!-- When checkbox is checked -->
<label class="someclasslabel" on-click="[[event]]">
    <span>Male</span>
    <input type="checkbox" data-bind="checked: $properties.value">
    <span class="checkbox">
::after
</span>
</label>

我想要一个定位器,这样我就可以获取属性,如果它被选中或未选中,然后如果已经选中则取消选中它。

【问题讨论】:

标签: html css selenium xpath


【解决方案1】:

您可以尝试在您的复选框元素上执行一些 Javascript 来获取值。

after_value = driver.execute_script
    ("return window.getComputedStyle(document.querySelector('.someclasslabel'),':after')
    .getPropertyValue('content');")

这有点小技巧,但值得一试。

【讨论】:

    【解决方案2】:

    在获得所有复选框元素(使用相同的选择器)之后,无论它们是否被选中,您都可以使用循环遍历它们,并使用selected 方法检查它们是否被选中。 (此处的 C# 参考 https://seleniumhq.github.io/selenium/docs/api/dotnet/ 用于 RemoteWebElement.Selected 属性)。 如果.selected == true -》点击;如果没有被选中,什么都不做;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2020-08-26
      相关资源
      最近更新 更多