1

Selenium can't find the input field neither with Id selector nor with XPath selector.

I tried to search for if it's a hidden div or body

webDriver.FindElement(By.XPath("//*[@id='64inputText']")).SendKeys(cnpj);
webDriver.FindElement(By.Id("64inputText")).SendKeys(cnpj);

Here's the source code:

<input id="64inputText" name="64inputText" type="text" class="form-control pd-input-size ng-valid-pattern ng-valid-minlength ng-valid-maxlength ng-valid-cpf ng-valid-cnpj ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" ng-model="ngModel" ng-required="ngRequired" ng-change="onChange($event)" ng-blur="onBlur($event)" ng-cut="naoPermitirCortar === true ? $event.preventDefault() : ngCut()" ng-copy="naoPermitirCopiar === true ? $event.preventDefault() : ngCopy()" ng-paste="naoPermitirColar === true ? $event.preventDefault() : ngPaste()" ng-disabled="ngDisabled" ng-readonly="ngReadonly" placeholder="" maxlength="500" minlength="" pd-maiusculo="::maiusculo" required="required" aria-invalid="false" style="">

and the link.

orde
  • 5,233
  • 6
  • 31
  • 33
  • I don't find the element that you mentioned in your post in the given url. Do we have to navigate to some other page? – supputuri Jul 25 '19 at 18:48
  • oh sorry, its the "CNPJ/CPF*" input field. – Thomas Mendes Jul 25 '19 at 18:51
  • It has ID `61inputText` when I view that page. Also, that portion of the page takes a considerably long time to load, much longer than the rest of the page. That might be part of your problem. – JLRishe Jul 25 '19 at 19:03
  • It's part of an iframe, so you should switch to iframe before interacting with the elements in the iframe. – supputuri Jul 25 '19 at 19:15
  • Welcome to SO. Please take the time to read [How to Ask]. It will help you craft solid questions that will hopefully get useful answers. – orde Jul 25 '19 at 20:29
  • 1
    Possible duplicate of [Selenium: Unable to access iframe and data inside it](https://stackoverflow.com/questions/9607964/selenium-unable-to-access-iframe-and-data-inside-it) – orde Jul 25 '19 at 20:31

1 Answers1

1

Here is the logic that you have to use.

# switch to the first iframe
driver.switch_to.frame(driver.find_elements_by_xpath("//iframe")[0])
# then interact with the field (Make sure you enter only numeric in this field)
driver.find_element_by_xpath("//input[@id='61inputText']").send_keys("12345678901")
# Don't forget to switch to parent window once you are done with actions on iframe using below
driver.switch_to.default_content()
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • 1
    Your first line can be just `driver.switch_to.frame(0)`, see [the docs](https://seleniumhq.github.io/selenium/docs/api/py/_modules/selenium/webdriver/remote/webdriver.html#WebDriver.switch_to). Also, if you are going to just use the first element from a `.find_elements_*` call, just use the singular form, `.find_element_*`. If you are going to search for an element by ID, then use `.find_element_by_id()`. There's no reason to use XPath in that case. – JeffC Jul 26 '19 at 00:55