Selenium化合物类名称[无法插入值]

时间:2017-11-16 14:21:40

标签: c# selenium selenium-chromedriver

我目前很难在一个Frame中的字段中插入一个值,我尝试删除类名中的空格但是它没有工作

class = " ng-pristine ng-valid ng-scope ng-empty input-sm form-control nc-pristine ng-valid-required ng-touching"



<textarea ng-if="$ctrl.fieldType.isTextarea($ctrl.field.fieldType, $ctrl.field.isHtmlFormatted)" ng-model="$ctrl.field.value" ng-change="$ctrl.onChange()" ng-required="$ctrl.field.isRequired" ng-class="{
    'input-sm': true,
    'form-control': true,
    'nc-pristine': $ctrl.getPristine(),
    'nc-not-pristine': $ctrl.getNotPristine()
   }" class="ng-pristine ng-valid ng-scope ng-empty input-sm form-control nc-pristine ng-valid-required ng-touched"></textarea>
&#13;
&#13;
&#13;

我在Chrome Developer工具中尝试使用此javascript代码

Chrome控制台开发者Javascript

&#13;
&#13;
document.getElementsByClassName('ng-pristine ng-valid ng-scope ng-empty input-sm form-control nc-pristine ng-valid-required ng-touched')[0].value='Hello World';
&#13;
&#13;
&#13;

在Selenium中,我尝试了这两种方式,但没有一种方法可以工作:

使用Javascript

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("document.getElementsByClassName('ng-pristine ng-valid ng-scope ng-empty input-sm form-control nc-pristine ng-valid-required ng-touched')[0].value='Hello World';");

错误消息:

 System.InvalidOperationException occurred
  HResult=0x80131509
  Message=unknown error: Cannot set property 'value' of undefined
  (Session info: chrome=62.0.3202.94)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64)
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
   at ConsoleApp5.Program.Main(String[] args) 

使用cssSelector,删除类名中的间距并用点替换

driver.FindElement(By.CssSelector("ng-pristine.ng-valid.ng-scope.ng-empty.input-sm.form-control.nc-pristine.ng-valid-required.ng-touched")).SendKeys("Hello World");

错误消息

  OpenQA.Selenium.NoSuchElementException occurred
  HResult=0x80131500
  Message=no such element: Unable to locate element: {"method":"css selector","selector":"ng-pristine.ng-valid.ng-scope.ng-empty.input-sm.form-control.nc-pristine.ng-valid-required.ng-touched"}
  (Session info: chrome=62.0.3202.94)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64)
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByCssSelector(String cssSelector)
   at OpenQA.Selenium.By.<>c__DisplayClass23_0.<CssSelector>b__0(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)

3 个答案:

答案 0 :(得分:2)

鉴于提供的HTML,请尝试:

driver.findElement(By.cssSelector("div > div > nc-editable-field > textarea"));

另外,您说该元素在Frame中,您确定要正确切换到该帧吗?

答案 1 :(得分:0)

以下是更多HTML代码

break;

答案 2 :(得分:0)

尝试以下代码块:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("document.getElementsByClassName('ng-pristine ng-valid ng-scope ng-empty input-sm form-control nc-pristine ng-valid-required ng-touched')[0].click()");
js.ExecuteScript("document.getElementsByClassName('ng-pristine ng-valid ng-scope ng-empty input-sm form-control nc-pristine ng-valid-required ng-touched')[0].value='Hello World'");