0

Hi I've been trying to fill a number in an input box in Chrome (v 75.0.3770.142) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBE I've tried the below but get error message:

obj.FindElementById("cartPrdQtyBtn0").Value = ("100000")
obj.FindElementByCss("input.form-control.ng-pristine.ng-untouched.ng- 
invalid.ng-invalid-required#cartPrdQtyBtn0").SendKeys ("10000")
obj.FindElementByXPath("//input[@class='form-control ng-pristine ng- 
untouched ng-invalid ng-invalid-required' and 
@id='cartPrdQtyBtn0']").SendKeys ("100000") 

(1) HTML before clicking within the input element:

<div class="form-group" ng-class="{'has-error': 
(entryItem.invalidProductQuantity || entryItem.invalidPallet || 
entryItem.pumpingQtyError || entryItem.lineItemQtyError)}" ng- 
hide="entryItem.isPalletEnabled || entryItem.isCancelled"><!-- ngIf: !entryItem.isDecimal --><input id="cartPrdQtyBtn0" type="text"class="form-control ng-pristine ng-untouched ng-invalid ng-invalid- 
required" 
  restrict="number" restrict-max="100000" required="" ng- 
  model="entryItem.productDisplayQuantity" ng- 
  readonly="entryItem.isReadOnly" 
  ng-blur="updateCartProduct(entryItem, $index)" ng- 
  if="!entryItem.isDecimal"> 
  <!-- end ngIf: !entryItem.isDecimal -->
  <!-- ngIf: entryItem.isDecimal -->
  </div>
  <p ng-bind="entryItem.productDisplayQuantity" ng-show="entryItem.isCancelled" class="ng-hide"></p>

(2) HTML after clicking within the input element:

  <div class="form-group has-error" ng-class="{'has-error': 
  (entryItem.invalidProductQuantity || entryItem.invalidPallet || 
  entryItem.pumpingQtyError || entryItem.lineItemQtyError)}" ng- 
  hide="entryItem.isPalletEnabled || entryItem.isCancelled">
  <!-- ngIf: !entryItem.isDecimal -->
  <input id="cartPrdQtyBtn0" type="text"
  class="form-control ng-pristine ng- 
  invalid ng-invalid-required ng-touched" restrict="number" restrict- 
  max="100000" required="" ng-model="entryItem.productDisplayQuantity" ng- 
  readonly="entryItem.isReadOnly" ng-blur="updateCartProduct(entryItem, 
  $index)" ng-if="!entryItem.isDecimal">
  <!-- end ngIf: !entryItem.isDecimal -->
  <!-- ngIf: entryItem.isDecimal -->
  </div>
  <p ng-bind="entryItem.productDisplayQuantity" ng-show="entryItem.isCancelled" class="ng-hide"></p>

(3) HTML after sending some text manually (100000):

  <div class="form-group" ng-class="{'has-error': 
  (entryItem.invalidProductQuantity || entryItem.invalidPallet || 
  entryItem.pumpingQtyError || entryItem.lineItemQtyError)}" ng- 
  hide="entryItem.isPalletEnabled || entryItem.isCancelled">
  <input id="cartPrdQtyBtn0" type="text" class="form-control ng-pristine
  ng-untouched ng-valid ng-valid-required" restrict="number" restrict- 
  max="100000" required="" ng-model="entryItem.productDisplayQuantity" ng- 
  readonly="entryItem.isReadOnly" ng-blur="updateCartProduct(entryItem, 
  $index)" ng-if="!entryItem.isDecimal">
 <p ng-bind="entryItem.productDisplayQuantity" ng-show="entryItem.isCancelled" class="ng-hide">100000</p>
DWP
  • 103
  • 1
  • 8
  • 1
    Not sure if you can use `.SendKeys` like that - I could be wrong, but what I would advise is making sure that if you are using that method that your browser window is active and read here for more: https://docs.microsoft.com/en-us/office/vba/api/excel.application.sendkeys and here https://developer.mozilla.org/en-US/docs/Web/API/Document – Dean Jul 19 '19 at 07:40

2 Answers2

2

To send a character sequence within the input field you can use either of the following Locator Strategies:

  • cssSelector:

    obj.FindElementByCss("input.form-control.ng-pristine.ng-untouched.ng-invalid.ng-invalid-required[id^='cartPrdQtyBtn']").Click
    obj.FindElementByCss("input.form-control.ng-pristine.ng-invalid.ng-invalid-required.ng-touched[id^='cartPrdQtyBtn']").SendKeys("10000")
    
  • xpath:

    obj.FindElementByXPath("//input[@class='form-control ng-pristine ng-untouched ng-invalid ng-invalid-required' and starts-with(@id, 'cartPrdQtyBtn')]").Click
    obj.FindElementByXPath("//input[@class='form-control ng-pristine ng-invalid ng-invalid-required ng-touched' and starts-with(@id, 'cartPrdQtyBtn')]").SendKeys("10000")
    

Note: As it is a dynamic element you need to induce a waiter for the element to be clickable


Reference

You can find a couple of relevant discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Debanjan, I'm getting Run-time error '0': SeleniumError element not interactable (Session info: chrome=75.0.3770.142) (Driver info: chromedriver =75.0.3770.140...) – DWP Jul 19 '19 at 08:09
  • @DWP Checkout the updated answer and let me know the status. – DebanjanB Jul 19 '19 at 08:14
  • both the FindElementByXPath and FindElementByCss are throwing Compile error:Syntax error – DWP Jul 19 '19 at 08:21
  • @DWP Checkout the updated answer and let me know the status. – DebanjanB Jul 19 '19 at 08:54
  • Hi DebanjanB, the code still can’t find the element - it’s not the wait time since I’m working line by line in debug mode in VBE and the page has enough time before executing each line. Also checked that the right page is active with debug.print. The strange thing is if I point and click the input box manually to let the dynamic class change then sendkeys with the new class it works. But without manual clicking the input box the page won’t allow to .click or .sendkeys that element, pls do help if anything else comes to mind. Could it be the ng-readonly for example? – DWP Jul 19 '19 at 11:25
  • @DWP Can you update the question with 3 HTMLs? 1) Before clicking within the input element. 2) After clicking within the input element and 3) After sending some text manually? – DebanjanB Jul 19 '19 at 12:20
  • I've updated the question with the 3 HTMLs thank you kindly for the much needed assistance – DWP Jul 19 '19 at 14:29
  • @DWP Check out the updated answer and let me know the status – DebanjanB Jul 19 '19 at 14:50
  • I'm getting Run-time error'11': ElementNotVisibleError element not interactable (Session info: chrome=75.0.3770.142)(Driver info: chromedriver=75.0.3770.140)(2d9f97485c7b07dc18a74666574f19176731995c-refs/branch-heads/3 NT 10.0.17763 x86_64) – DWP Jul 19 '19 at 15:07
  • Maybe because Chrome is version 75.0.3770.142 and ChromeDriver is version 75.0.3770.140 it's slightly different and incompatible? Unfortunately http://chromedriver.chromium.org/downloads only has for Chrome v.75 and Chrome v.76 but nothing in between – DWP Jul 19 '19 at 15:09
  • 1
    @DWP As both are on **75.x** so no issues. Is the url public? – DebanjanB Jul 19 '19 at 15:11
  • this is private url – DWP Jul 19 '19 at 15:13
  • I will try .click on nearby element then sendkeys tab as workaround – DWP Jul 19 '19 at 15:15
  • i added the div class before the input class and both .click and .sendkeys work now, thanks so much for the help as I learned the "id^=" in cssselector and "starts-with" for xpath in this thread – DWP Jul 21 '19 at 05:13
1

finally figured out that it works with the div class also in front:

obj.FindElementByXPath("//div[@class='form-group']/input[@class='form-control ng-pristine ng-untouched ng-invalid ng-invalid-required'and @id= 'cartPrdQtyBtn0']").SendKeys ("100000")
DWP
  • 103
  • 1
  • 8