NoSuchElementException,Selenium 无法定位元素 [英] NoSuchElementException, Selenium unable to locate element

查看:49
本文介绍了NoSuchElementException,Selenium 无法定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我试过了:

 driver.findElement(By.id("originTextField"))

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

请帮助我,我将不胜感激.

这是html:

解决方案

NoSuchElementException

org.openqa.selenium.NoSuchElementException 俗称 NoSuchElementException 扩展 org.openqa.selenium.NotFoundException 这是一种 WebDriverException.

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

  • 使用 WebDriver.findElement(By by) 时:

    //示例:WebElement my_element = driver.findElement(By.xpath("//my_xpath"));

  • 使用 WebElement.findElement(By by) 时:

    //示例:WebElement my_element = element.findElement(By.xpath("//my_xpath"));

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

常量字段类型值SESSION_ID public static final java.lang.String "Session ID"例如(会话信息:chrome=63.0.3239.108)DRIVER_INFO public static final java.lang.String "Driver info"例如(驱动程序信息:chromedriver=2.34.522940(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),平台=Windows NT 6.1.7601 SP1 x86)BASE_SUPPORT_URL 保护静态最终 java.lang.String "http://seleniumhq.org/exceptions/"例如(有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html)

<小时>

原因

NoSuchElementException 的原因可能是以下之一:

  • 您采用的定位器策略未识别HTML DOM.
  • 您采用的定位器策略无法识别该元素,因为它不在浏览器的视口.
  • 您采用的定位器策略可识别元素,但由于存在属性style="display: none;"而无法显示.
  • 您采用的定位器策略不能唯一地识别HTML DOM中所需的元素,并且当前会找到其他一些隐藏/不可见元素.
  • 您尝试定位的 WebElement 位于