【问题标题】:Send value to a ng-class向 ng-class 发送值
【发布时间】:2019-12-14 15:28:37
【问题描述】:

我在向具有 ng-class 的元素发送值时遇到问题

我已经尝试过这个driver.FindElement(By.Id("limit")).SendKeys("10.00");,但似乎无法识别该元素。也试过了-

driver.FindElement(By.Id("limit_display")).SendKeys("10.00");


<td class="center">
   <amount-input ng-class="{invalid: !addNewCardForm.limit.$valid}"  
            name="limit" amount="newCard.limit" required="" 
            class="ng-isolate-scope">
      <div class="amount-input">
        <div class="currency" ng-class="{degrade: degrade}">$</div>
        <input id="limit_display" name="limit_display" type="text" 
            value="0.00" ng-focus="onFocus()" required="" disable-animate=""> 
        <input id="limit" ng-class="{edit: edit || degrade}" 
            class="entry ng-pristine ng-not-empty ng-valid ng-valid-required ng-touched" 
            name="limit" type="number" ng-model="amount" 
            ng-blur="onBlur()" ng-keydown="onKeyDown($event)" required=""  
            disable-animate="">
      </div>
   </amount-input>
</td>

我希望发送一个值

【问题讨论】:

  • ng-class 是一个 AngularJS 指令,因此您不会在生成的 DOM 中找到它。
  • 另一种查找方式?

标签: c# selenium xpath css-selectors webdriverwait


【解决方案1】:

请检查您的元素是否在框架内,如果在框架内则切换到框架然后尝试对元素进行操作 -

你可以试试 xpath -

driver.FindElement(By.xpath(".//input[@name='limit_display' and @ng-focus='onFocus()']")).SendKeys("10.00");

如果仍然无法正常工作,您可以尝试动作类 -

Actions actions = new Actions(driver);
    actions.moveToElement(element).sendKeys("10.00").build().perform();

【讨论】:

    【解决方案2】:

    所需的元素是Angular 元素,因此您必须为所需的ElementToBeClickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies 作为解决方案:

    • CssSelector:

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.entry.ng-pristine.ng-not-empty.ng-valid.ng-valid-required.ng-touched[id='limit'][name='limit']"))).SendKeys("10.00");
      
    • XPath:

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='entry ng-pristine ng-not-empty ng-valid ng-valid-required ng-touched' and @id='limit'][@name='limit']"))).SendKeys("10.00");
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-11
      • 2014-07-08
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 2014-10-03
      相关资源
      最近更新 更多