【问题标题】:Can't find input element by Id or XPath无法通过 Id 或 XPath 找到输入元素
【发布时间】:2019-12-04 02:23:58
【问题描述】:

Selenium 无法使用 Id 选择器和 XPath 选择器找到输入字段。

我试图搜索它是隐藏的div 还是body

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

这里是源代码:

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

【问题讨论】:

  • 我在给定的网址中找不到您在帖子中提到的元素。我们必须导航到其他页面吗?
  • 对不起,它是“CNPJ/CPF*”输入字段。
  • 当我查看该页面时,它的 ID 为 61inputText。此外,页面的该部分需要相当长的时间来加载,比页面的其余部分要长得多。这可能是您的问题的一部分。
  • 它是 iframe 的一部分,因此在与 iframe 中的元素交互之前,您应该切换到 iframe。

标签: c# html angular selenium selenium-webdriver


【解决方案1】:

这是你必须使用的逻辑。

# 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()

【讨论】:

  • 你的第一行可以是driver.switch_to.frame(0),见the docs。此外,如果您打算只使用.find_elements_* 调用中的第一个元素,只需使用单数形式.find_element_*。如果要按 ID 搜索元素,请使用 .find_element_by_id()。在这种情况下没有理由使用 XPath。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多