如何使用c#通过Selenium将文本发送到text-angular标签内的输入标签

时间:2018-04-05 12:41:28

标签: c# angular selenium selenium-webdriver xpath

以下是我尝试将selenium测试发布到其中的代码....这是我正在使用的代码,

 IWebElement xxvalue = webElement.FindElement(By.Id("emailMessageContent"));
 xxvalue.SendKeys("This is only a test");

我已经尝试了其中的许多元素来查找并发送测试消息,但没有一个有效。输入是'隐藏'..所以它找不到该字段并输入它...所以有人遇到这个并且可以告诉我如何将信息发布到textarea或输入?等。

<div>
  <h3>Email Message Content</h3>
  <text-angular name="emailMessageContent" class="email-content-wysiwyg ng-untouched ng-valid ng-isolate-scope ta-root ng-dirty ng-valid-parse focussed" ta-disabled="!vm.selectedEmailEditable()" ng-model="vm.selectedEmail.CustomText"
    ta-toolbar="[['bold','italics','underline','insertLink'],['ul','ol','indent','outdent']]" id="emailMessageContent">
    <div text-angular-toolbar="" name="textAngularToolbar4562419773867687" ta-toolbar="[['bold','italics','underline','insertLink'],['ul','ol','indent','outdent']]" class="ng-scope ng-isolate-scope ta-toolbar btn-toolbar focussed">
      <div class="btn-group"><button type="button" class="btn btn-default ng-scope" name="bold" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Bold"><i class="fa fa-bold"></i></button>
        <button
          type="button" class="btn btn-default ng-scope" name="italics" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Italic"><i class="fa fa-italic"></i></button><button type="button" class="btn btn-default ng-scope" name="underline" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Underline"><i class="fa fa-underline"></i></button>
          <button
            type="button" class="btn btn-default ng-scope" name="insertLink" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Insert / edit link"><i class="fa fa-link"></i></button>
      </div>
      <div class="btn-group"><button type="button" class="btn btn-default ng-scope" name="ul" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Unordered List"><i class="fa fa-list-ul"></i></button>
        <button
          type="button" class="btn btn-default ng-scope" name="ol" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Ordered List"><i class="fa fa-list-ol"></i></button><button type="button" class="btn btn-default ng-scope" name="indent" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Increase indent"><i class="fa fa-indent"></i></button>
          <button
            type="button" class="btn btn-default ng-scope" name="outdent" unselectable="on" ng-disabled="isDisabled()" tabindex="-1" ng-click="executeAction()" ng-class="displayActiveToolClass(active)" title="Decrease indent"><i class="fa fa-outdent"></i></button>
      </div>
    </div>
    <div class="ta-scroll-window ng-scope ta-text ta-editor form-control" ng-hide="showHtml">
      <div class="popover fade bottom" style="max-width: none; width: 305px;">
        <div class="arrow"></div>
        <div class="popover-content"></div>
      </div>
      <div class="ta-resizer-handle-overlay">
        <div class="ta-resizer-handle-background"></div>
        <div class="ta-resizer-handle-corner ta-resizer-handle-corner-tl"></div>
        <div class="ta-resizer-handle-corner ta-resizer-handle-corner-tr"></div>
        <div class="ta-resizer-handle-corner ta-resizer-handle-corner-bl"></div>
        <div class="ta-resizer-handle-corner ta-resizer-handle-corner-br"></div>
        <div class="ta-resizer-handle-info"></div>
      </div>
      <div id="taTextElement4562419773867687" contenteditable="true" ta-bind="ta-bind" ng-model="html" ta-readonly="disabled" class="ng-valid ta-bind ng-dirty ng-valid-parse ng-touched" style=""> </div>
    </div><textarea id="taHtmlElement4562419773867687" ng-show="showHtml" ta-bind="ta-bind" ng-model="html" ta-readonly="disabled" class="ng-pristine ng-untouched ng-valid ng-scope ta-bind ta-html ta-editor form-control ng-hide"></textarea><input type="hidden"
      tabindex="-1" style="display: none;" name="emailMessageContent" value=""></text-angular>
</div>

3 个答案:

答案 0 :(得分:0)

如果元素被隐藏,请尝试:

IWebElement xxvalue =  webElement.FindElement(By.Id("emailMessageContent"));

IJavascriptExecutor JE = (IJavascriptExecutor)driver; //your webdriver instance here

//here you define the string to make the element visible
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";

JE.ExecuteScript(js, xxvalue);

xxvalue.SendKeys("This is only a test");

答案 1 :(得分:0)

由于您要将文本发送到<input>标记,要实现这一点,您可以使用以下代码块:

IWebElement xxvalue = driver.FindElement(By.XPath("//textarea[@class='ng-pristine ng-untouched ng-valid ng-scope ta-bind ta-html ta-editor form-control ng-hide']/input[@name='emailMessageContent']"));
String js = "arguments[0].style.display='block'; arguments[0].type='text';";
((IJavascriptExecutor) driver).ExecuteScript(js, xxvalue);
xxvalue.SendKeys("This is only a test");

注意:我的答案中的 WebDriver 实例是驱动程序

作为替代方案,您还可以尝试以下代码块:

IWebElement xxvalue = driver.FindElement(By.XPath("//textarea[@class='ng-pristine ng-untouched ng-valid ng-scope ta-bind ta-html ta-editor form-control ng-hide']/input[@name='emailMessageContent']"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].removeAttribute('style')", xxvalue)
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].type='text';", xxvalue);
xxvalue.SendKeys("This is only a test");

答案 2 :(得分:0)

我把光标放在了场上,但当我试图发送密钥时,它说它无法专注于那个对象,这很奇怪......但是我遇到了这个Selenium命令&#39; Actions& #39 ;.使用它,它对我有用。

            IWebElement messageContent = Driver.Instance.FindElement(By.Id("emailMessageContent"));
            Actions actions = new Actions(Driver.Instance);
            actions.MoveToElement(messageContent);
            actions.Click();
            actions.SendKeys("This is a test");
            actions.Build().Perform();