NoSuchElementException-无法找到元素 [英] NoSuchElementException - Unable to locate element
问题描述
我有一个输入框,例如我在这里使用的输入框,其HTML是
I have an input box like the one I am using here to enter my question, and whose HTML is
<body id="tinymce" class="mce-content-body" contenteditable="true" onload="window.parent.tinymce.get('Description').fire('load');" spellcheck="false" style="padding-bottom: 50px; padding-left: 1px; padding-right: 1px; overflow-y: hidden;">
<p>
<br data-mce-bogus="1"/>
</p>
</body>
每次,我都尝试输入一些文字
Every-time, I try to enter some text to it
@FindBy(xpath="//body[@id='tinymce']") WebElement Category_Body;
Category_Body.sendKeys("Android Smart Phone - 16GB");
我得到了错误-
org.openqa.selenium.NoSuchElementException:无法找到元素:{"method":"xpath","selector":"//body [@ id ='tinymce']"}
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//body[@id='tinymce']"}
推荐答案
如果将NoSuchElementException
作为提供的异常,则可能有以下原因:-
If you are getting NoSuchElementException
as your provided exception, There may be following reasons :-
-
可能是当您要查找元素时,它不会出现在
DOM
上,因此您应实现WebDriverWait
,直到元素可见,如下所示:-
May be when you are going to find element, it would not be present on the
DOM
, So you should implementWebDriverWait
to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tinymce")));
Category_Body.sendKeys("Android Smart Phone - 16GB");
可能是此元素在任何frame
或iframe
内部.如果是的话,则需要在找到以下元素之前切换frame
或iframe
:-
May be this element is inside any frame
or iframe
. If it is, you need to switch that frame
or iframe
before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
//Now find the element
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tinymce")));
Category_Body.sendKeys("Android Smart Phone - 16GB");
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
这篇关于NoSuchElementException-无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!