NoSuchElementException,Selenium 无法定位元素 [英] NoSuchElementException, Selenium unable to locate element
问题描述
我想在 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 位于
标记内.
- WebDriver 实例甚至在 HTML DOM 中的元素存在/可见之前就在寻找 WebElement.
解决方案
解决 NoSuchElementException 的解决方案可以是以下任一方式:
采用 定位器策略标识所需的 WebElement.您可以借助 Developer Tools(Ctrl+Shift+I 或 F12) 并使用元素检查器.
使用
executeScript()
方法滚动元素查看如下:WebElement elem = driver.findElement(By.xpath("element_xpath"));((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
Incase 元素具有style="display: none;" 属性,通过
executeScript()
方法删除该属性如下:WebElement element = driver.findElement(By.xpath("element_xpath"));((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)element.sendKeys("text_to_send");
要检查元素是否在
中,向上遍历 HTML 以定位相应的
> 通过以下任一方法标记和
switchTo()
所需的 iframe :driver.switchTo().frame("frame_name");driver.switchTo().frame("frame_id");driver.switchTo().frame(1);//1 代表帧索引
在这里您可以找到关于Selenium Webdriver Java 中是否可以在不使用 driver.switchTo().frame(frameName") 的情况下切换到框架中的元素?.p>
如果元素在 HTML DOM 中不存在/可见,则立即引入 WebDriverWait 带有 ExpectedConditions 设置为适当的方法如下:
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
等待visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
参考
您可以找到Selenium的python 基于客户端的相关讨论:
i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).
I tried:
driver.findElement(By.id("originTextField"))
or by xPath and cssSelector String generated by chrome in dev tools.
Please help me, i would appreciate explanation.
this is html:
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using
WebDriver.findElement(By by)
://example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using
WebElement.findElement(By by)
://example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
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)
Reason
The reason for NoSuchElementException can be either of the following :
- The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
- The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
- The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
- The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
- The WebElement you are trying to locate is within an
<iframe>
tag. - The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use
executeScript()
method to scroll the element in to view as follows :WebElement elem = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through
executeScript()
method as follows :WebElement element = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element) element.sendKeys("text_to_send");
To check if the element is within an
<iframe>
traverse up the HTML to locate the respective<iframe>
tag andswitchTo()
the desired iframe through either of the following methods :driver.switchTo().frame("frame_name"); driver.switchTo().frame("frame_id"); driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame("frameName") in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
这篇关于NoSuchElementException,Selenium 无法定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!