硒获取属性/ CSS /属性

时间:2018-12-11 14:17:55

标签: c# selenium selenium-webdriver webdriver

我遇到的情况是,硒需要打开登录表单,并且在页面加载后立即单击“登录”而不输入任何内容。当什么都没有填写时,两个字段都是:

  • 周围会突出显示为红色
  • 字段名称颜色已更改为红色
  • “必填”字符串出现在字段的底部

我已经运行了整个测试,找到了字段,但是我不知道如何正确检查(这些点)。

仅针对一个字段的DOM (对于非红色形式)非常难看:

<div class="form-row" xpath="1">
   <div class="jss98 jss99 primary input outlined-input" input="[object Object]" meta="[object Object]">
      <label class="jss113 jss102 jss107 jss110 jss112" data-shrink="false" for="email">Email</label>
      <div class="jss133 jss120 jss134">
         <fieldset aria-hidden="true" class="jss150 jss127" style="padding-left: 29.25px;">
            <legend class="jss151" style="width: 0.01px;"><span>​</span></legend>
         </fieldset>
         <input aria-invalid="false" class="jss143 jss128" id="email" type="text" value="" style="background-image: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAjZJREFUWAntV7vKIlEMPt5dOy/NID9WIoKKCAsiWIqFFmLra4j2IoKPsL24YKWFD7Ai+ASClY2yNioWgjckfzLrCeN46XSKNXDmJPkyyWdOZnCE+CdfuP3G9RcXvHhRDapFNVUhZYXr1YX1+anmlwUvv3D9xPVu+YEFFRNeqCXKu6tf6i2IALXGMDEbVvlS+EPg/+tAuVwWmUyGR+/tHSgUCiKdThtHgCs/egosFouIRCIiEAjoY9n2+XwikUgIp9PJPq1itVpFLBYTfr9f636o8zs6GAzCeDwGKb1eD1wuF+OYARqNBpzPZzVkuVxCNpu9wuPxOEynU5kCWq0W2Gw2jhkOh1Cr1dgmVmyMRiO+USrNZpNxPD/p5n29XoPb7VZjTCYTTCYTxqRSqVQ4h54AD6HX6xXJZPKmTblcjn35fJ51qWBxkUqlVBM7KEKhkIR41+Zg50VhAofDQSBjPS72+z37tDo7Udntdqopdy1G+qP7CGMC2+1WdLtd8l0JniHb7Xb7hiSet8CjU2Nms5kYDAYcLxVtDunT7nw+Ho8HOp0OYDdgs9lAvV4Hs9nMON4EpVIJ5vO5Oog0M+Fw+ApXFAX6/T4cj0dYrVZQrVavcP0MEJGrALJpammg7mHS53A4nuJ2u/0uridgxYQ3cjqdbnx6B83MM8EOPIMZ4xlgjwHK3VYhj5f4i8UiRKNRzv35S2b4DHwIUAcWBjx5suSCCPyRlgG7WtvQj1P5o4kEfTK//fP8G/4x2UymA12DAAAAAElFTkSuQmCC&quot;); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;">
      </div>
   </div>
</div>

带有“红色”的DOM:

<div class="form-row" xpath="1">
   <div class="jss98 jss99 primary input outlined-input error " input="[object Object]" meta="[object Object]" aria-describedby="email-helper-text">
      <label class="jss113 jss116 jss105 jss102 jss107 jss110 jss112" data-shrink="false" for="email">Email</label>
      <div class="jss133 jss120 jss139 jss125 jss134">
         <fieldset aria-hidden="true" class="jss150 jss127" style="padding-left: 29.25px;">
            <legend class="jss151" style="width: 0.01px;"><span>​</span></legend>
         </fieldset>
         <input aria-invalid="true" class="jss143 jss128" id="email" type="text" value="" style="background-image: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAmJJREFUWAntV7uKIkEUvbYGM4KID3wEIgjKRLLpKGLgFwiCfslGhkb7IbLgAzE1GhMxWxRRBEEwmEgDERWfW6fXuttq60a2wU6B1qlzb9U5fatsKROJVigUArvd7oeAyePx6Af3qGYymT7F2h8Wi+V7Pp+fmE7iv4Sw81GieusKIzNh4puCJzdaHIagCW1F4KSeQ4O4pPLoPb/3INBGBZ7avgz8fxWIxWIUCoX43Blegbe3NwoGg88zwMoncFUB8Yokj8dDdrv9MpfHVquV/H4/iVcpc1qgKAp5vV6y2WxaWhefreB0OimXy6kGkD0YDKhSqdB2u+XJqVSK4vE4QWS5XKrx0WjEcZ/PR9lslhwOh8p1Oh2q1Wp0OBw4RwvOKpBOp1kcSdivZPLvmxrjRCKhiiOOSmQyGXp5ecFQbRhLcRDRaJTe39//BHW+2cDr6ysFAoGrlEgkwpwWS1I7z+VykdvtliHuw+Ew40vABvb7Pf6hLuMk/rGY02ImBZC8dqv04lpOYjaw2WzUPZcB2WMPZet2u1cmZ7MZTSYTNWU+n9N4PJbp3GvXYPIE2ADG9Xqder2e+kTr9ZqazSa1222eA6FqtUoQwqHCuFgscgWQWC6XaTgcEiqKQ9poNOiegbNfwWq1olKppB6yW6cWVcDHbDarIuzuBBaLhWrqVvwy/6wCMnhLXMbR4wnvtX/F5VxdAzJoRH+2BUYItlotmk6nLGW4gX6/z+IAT9+CLwPPr8DprnZ2MIwaQBsV+DBKUEfnQ8EtFRdFneBDKWhCW8EVGbdUQfxESR6qKhaHBrSgCe3fbLTpPlS70M0AAAAASUVORK5CYII=&quot;); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%;">
      </div>
      <p class="jss159 jss163 jss160" id="email-helper-text">Required</p>
   </div>
</div>

我们可以看到:“ class =“ jss98 jss99主要输入概述输入错误” / aria- describebyby =“ email-helper-text”,输入aria-invalid =“ true”,最后在新段落中添加了文字“必填”

我现在想做的是在单击该字段之前检查该字段,并检查上面列出的那些内容是否为空,然后单击并检查现在是否存在以前不存在的元素。

有人可以解释一下标题中包含的三种方法如何变化吗?

我想取值吗? div中的类的名称(class =“ jss98 jss99主要输入概述输入”)。有人可以告诉我如何服用吗?

我正在用C#编写

2 个答案:

答案 0 :(得分:1)

您基本上可以使用XPath来检查class属性的内容,如下所示:

"//div[contains(@class, 'error')]"

这应该在您提交之前什么都找不到,在提交之后找到彩色元素。

答案 1 :(得分:0)

您要做的第一件事是将html之前和之后放在diff引擎中,以查看发生了什么变化。 (如果您还没有在线的话,可以在网上找到一个简单的。)我看到了三件事(我认为很重要):

  1. 将“错误”类添加到外部DIV
  2. INPUT上的“ aria-invalid”属性从false变为true
  3. P标记添加了id =“ email-helper-text”,并包含“必需”

之前和之后的测试将是这样的……您进行了.FindElements()(复数)操作,看看计数是之前的0还是之后的1。

这是一些代码示例

示例1

By errorClassLocator = By.CssSelector("div.outlined-input.error");

// before
Assert.Zero(Driver.FindElements(errorClassLocator).Count, "Verify that 'error' class is not present");

// after
Assert.NotZero(Driver.FindElements(errorClassLocator).Count, "Verify that 'error' class is present");

示例2

By ariaInvalidLocator = By.CssSelector("input#email[aria-invalid='true']");

// before
Assert.Zero(Driver.FindElements(ariaInvalidLocator).Count, "Verify that 'aria-invalid' attribute value of 'true' is not present");

// after
Assert.NotZero(Driver.FindElements(ariaInvalidLocator).Count, "Verify that 'aria-invalid' attribute is true");

注意:您还可以使用两个定位器...在不存在“ true”之前查找aria-invalid ='false'。另一种选择是找到元素,然后提取“ aria-invalid”的值并将其与“ true”或“ false”进行比较。

示例3

By requiredLocator = By.Id("email-helper-text");

// before
Assert.Zero(Driver.FindElements(requiredLocator).Count, "Verify that the 'email-helper-text' P is not present");

// after
Assert.NotZero(Driver.FindElements(requiredLocator).Count, "Verify that the 'email-helper-text' P is present");
Assert.AreEqual("Required", Driver.FindElement(requiredLocator).Text, "Verify that 'Required' text is present");

注意:对于以前的情况,使用.FindElements()是至关重要的,因为如果使用.FindElement(),则在找不到元素时会抛出该异常。如果未按预期出现,则在以后的情况下也很有用。您不会得到ElementNotFoundException,而是会得到失败的断言,并清楚地指出缺少的内容。