1

I try to send Keys to an input field via Java Selenium. I get the NoSuchElementException every time. I also tried everything from this Solution: NoSuchElementExeption, selenium unable to locate element. Thanks in Advance!

driver.findElement(By.xpath("//input[@class='pull-left ng-pristine ng-validng-empty ng-touched']")).sendKeys(t + Keys.ENTER);

<input class="pull-left ng-pristine ng-valid ng-empty ng-touched" ng-model="TagInputCtrl.tagInput" uib-typeahead="tagSuggestion for tagSuggestion in TagInputCtrl.getTagSuggestions($viewValue)" select-on-comma="" select-on-whitespace="" select-on-blur="" typeahead-focus-first="false" tag-select="TagInputCtrl.onEnter" tag-select-model="ngModel" sprd-max-input-length="50" ng-show="ngModel.length < TagInputCtrl.validatorOptions.tags.max" ng-focus="TagInputCtrl.focused = true" ng-blur="TagInputCtrl.focused = false" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-4377-3960" style="" type="text"/>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

To send a character sequence as the desired element is an Angular element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solution:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.pull-left.ng-pristine.ng-valid.ng-empty.ng-touched[ng-model^='TagInputCtrl']"))).sendKeys("Tim");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='pull-left ng-pristine ng-valid ng-empty ng-touched'][contains(@ng-model,'TagInputCtrl')]"))).sendKeys("Tim");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

There can be two things for NoSuchElementException :

  1. Your locator is not correct.

If this is the case then you can try with this xpath :

//input[contains(@class,'pull-left ng-pristine ng-valid ng-empty ng-touched') and @ng-model='TagInputCtrl.tagInput' and @ng-focus='TagInputCtrl.focused = true']  

Your webpage is integrated with Angular. So the provided xpath should work.

  1. Your input tag might be in iframe/frame/frameset.

If this is the case, then I would recommend you to switch the focus of your driver to the particular iframe, to interact with desire element.

for switching you can try this code :

driver.switchTo().frame(name_or_id)  

Generally, iframe tag contains name or id attribute, in case if both of them aren't available , you can go ahead with

driver.switchTo().frame(index)  

or

driver.switchTo().frame(iframe_element)  

Here, iframe_element is a web element.

Hope this helps.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38