How to verify if a check box is checked when ::after gets added once check box is checked

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

How to verify if a check box is checked when ::after gets added once check box is checked

问题

我正在尝试在Selenium代码中验证复选框是否被选中。复选框在被选中后会添加::after伪元素,但我无法验证该伪元素是否存在。有人能帮我解决这个问题吗?我无法找到一个正确的JavaScript代码片段。

(图片已省略)

英文:

I'm trying to verify whether a check box is checked or not in the selenium code. It gets added ::after once the checkbox is checked but I'm not able to verify that pseudo element exist or not. Can anybody please help me to resolve that issue? I was not able to find a correct JavaScript snippet for that.

How to verify if a check box is checked when ::after gets added once check box is checked

答案1

得分: 0

你可以检查伪元素的属性。例如:

console.log(window.getComputedStyle(document.querySelector('h1'), '::after').getPropertyValue('content'));
<html>
  <head>
    <meta charset="UTF-8" />
    <style>
      h1::after {
        content: "new";
      }
    </style>
  </head>
  <body>
    <h1>site</h1>
  </body>
</html>
英文:

You could check pseudo element properties properties. E.g.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(window.getComputedStyle(document.querySelector(&#39;h1&#39;), &#39;::after&#39;).getPropertyValue(&#39;content&#39;));

<!-- language: lang-html -->

&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;style&gt;
      h1::after {
        content: &quot;new&quot;;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;site&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案2

得分: 0

你想要找到该元素并使用 isSelected。

isChecked = driver.findElement(By.xpath("label[class='labelContainer']/input")).isSelected();

英文:

You want to find the element and use isSelected.

isChecked = driver.findElement(By.xpath(&quot;label[class=&#39;labelContainer&#39;]/input&quot;)).isSelected();

huangapple
  • 本文由 发表于 2020年9月26日 00:57:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068376.html
匿名

发表评论

匿名网友

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

确定