Selenium Compound Class Name [unable to insert a value]

1

I am currently having a hard time inserting a value into a field which is within a Frame, I have tried removing the spaces in the class name but it didn't work

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

<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>

I have tried in Chrome Developer tool and it worked with this javascript code

Chrome Console Developper Javascript

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';

In Selenium I tried with these 2 ways none of them work:

Using 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';");

Error Message:

 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) 

Using cssSelector, removing the spacing in the Class Name and replacing it with dots

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");

Error Message

  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)
c#
selenium
selenium-chromedriver
asked on Stack Overflow Nov 16, 2017 by Sam • edited Nov 16, 2017 by Sam

3 Answers

2

Given the html provided try this:

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

Also, you stated that the element is in Frame, are you sure that you are switching to that frame correctly?

answered on Stack Overflow Nov 16, 2017 by smit9234
0

Here is more HTML Code below

<div class="row ng-scope" ng-repeat="field in $ctrl.getFields() as fields" ng-if="$index % 2 == 0">
       <!-- ngRepeat: i in [$index, $index + 1] --><!-- ngIf: fields[i] --><nc-form-group-template ng-repeat="i in [$index, $index + 1]" ng-if="fields[i]" section-id="a1gg0000002ARlMAAW" guid="a1gg0000002ARlMAAW338C1B4330ED4993A7B3178099C70517" field="fields[i]" context-id="a38g0000001juVQAAY" ,="" enable-field-access="$ctrl.enableFieldAccess" class="ng-scope ng-isolate-scope">
  <div class="form-group col-xs-6">
   <nc-field-label-template section-id="a1gg0000002ARlMAAW" field="$ctrl.field" inline-help-text="" label="Other Loan Purpose" is-required="$ctrl.field.isRequired" enable-field-access="$ctrl.enableFieldAccess" class="ng-isolate-scope">
  <div class="row content-label">
   <div class="col-xs-12">
    <span ng-show="$ctrl.inlineHelpText" popover="popover" data-toggle="popover" data-content="" class="fa fa-info-circle slds-show--inline ng-hide" data-original-title="" title="">
    </span>
    <label ng-bind="$ctrl.label" class="form-control-static input-sm slds-show--inline ng-binding" ng-class="{'is-required': $ctrl.isRequired, 'pull-left': $ctrl.enableFieldAccess}">Other Loan Purpose</label>
    <span class="slds-p-left--medium">
     <!-- ngIf: !$ctrl.field.isVisible && $ctrl.enableFieldAccess -->
     <span class="slds-assistive-text">This field will be hidden from the user.</span>
    </span>
    <!-- ngIf: $ctrl.enableFieldAccess -->
   </div>
  </div>
 </nc-field-label-template>
   <!-- ngIf: !$ctrl.isReadOnly() --><nc-editable-field-template ng-if="!$ctrl.isReadOnly()" section-id="a1gg0000002ARlMAAW" guid="a1gg0000002ARlMAAW338C1B4330ED4993A7B3178099C70517" field="$ctrl.field" context-id="a38g0000001juVQAAY" class="ng-scope ng-isolate-scope">
  <div class="row">
   <div class="col-xs-12 content-body">
    <nc-editable-field section-id="a1gg0000002ARlMAAW" guid="a1gg0000002ARlMAAW338C1B4330ED4993A7B3178099C70517" field="$ctrl.field" context-id="a38g0000001juVQAAY" class="ng-isolate-scope">
 
  <!-- ngIf: $ctrl.fieldType.isTextarea($ctrl.field.fieldType, $ctrl.field.isHtmlFormatted) --><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><!-- end ngIf: $ctrl.fieldType.isTextarea($ctrl.field.fieldType, $ctrl.field.isHtmlFormatted) -->

answered on Stack Overflow Nov 16, 2017 by Sam • edited Nov 16, 2017 by Sam
0

Try the following code block :

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'");
answered on Stack Overflow Nov 16, 2017 by DebanjanB

User contributions licensed under CC BY-SA 3.0