NoSuchElementExeption,selenium无法定位元素

Jan*_*ski 9 selenium webdriver css-selectors selenium-chromedriver selenium-webdriver

我想在selenium找到我的TextField,但我不知道如何(我第一次使用sellenium).

我试过了:

 driver.findElement(By.id("originTextField"))
Run Code Online (Sandbox Code Playgroud)

或者由xPath和cssSelector由开发工具中的chrome生成的字符串.

请帮助我,我会很感激的解释.

这是html:

在此输入图像描述

Deb*_*anB 19

NoSuchElementException异常

org.openqa.selenium.NoSuchElementException通常称为 NoSuchElementException,它扩展了 org.openqa.selenium.NotFoundException,这是一种 WebDriverException.

可以在以下两种情况下抛出NoSuchElementException:

根据JavaDocs,就像任何其他WebDriverException一样,NoSuchElementException应包含以下常量字段:

Constant Field      Type                                        Value
SESSION_ID          public static final java.lang.String        "Session ID"
e.g. (Session info: chrome=63.0.3239.108)

DRIVER_INFO         public static final java.lang.String        "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)

BASE_SUPPORT_URL    protected static final java.lang.String     "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
Run Code Online (Sandbox Code Playgroud)

原因

NoSuchElementException的原因可能是以下任一情况:

  • 您采用的定位器策略未标识HTML DOM中的任何元素.
  • 您采用的定位器策略无法识别元素,因为它不在浏览器的视口中.
  • 您采用的定位器策略标识元素,但由于属性style ="display:none;"的存在而不可见..
  • 您采用的定位器策略不能唯一标识HTML DOM中的所需元素,并且当前会找到其他隐藏/不可见元素.
  • 您尝试查找的WebElement位于<iframe>标记内.
  • webdriver的实例看着外面的WebElement甚至之前的元素都看得到内本/ HTML DOM.

解决NoSuchElementException的解决方案可以是以下任一种: