Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

excel - Need help to fill number into Chrome input box with Selenium

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...