码:
public void Test2() throws Exception{
Thread.sleep(5000);
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[@id='cboMenu/option[3]")).click();
}
Run Code Online (Sandbox Code Playgroud)
错误:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"cboMenu"}
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: host: 'venu-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 0f859bed-35df-4eba-a472-3bc2efec4814
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Run Code Online (Sandbox Code Playgroud)
请使用显式等待而不是Thread.sleep(5000),如下一个示例所示.它会为您提供更清晰的错误信息.
public void Test2() throws Exception{
new WebDriverWait(driver, 3).until(ExpectedConditions.visibilityOfElementLocated(By.id("cboMenu")))
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[@id='cboMenu/option[3]")).click();
}
Run Code Online (Sandbox Code Playgroud)
接下来,确认您的按钮未显示在不同的iFrame中.如果这样做,请将iFrame更改为您内部的元素:
driver.switchTo().frame("IFRAME_ID");
Run Code Online (Sandbox Code Playgroud)
IFRAME_ID取自DOM:
<iframe id="IFRAME_ID">
Run Code Online (Sandbox Code Playgroud)
您可以接下来更改visibilityOfElementLocated为presenceOfElementLocated,将验证DOM上是否存在元素,但并不一定意味着元素可见.通过您尝试单击的按钮,可以很好地了解您的webDriver是否在正确的范围内.
附加提示 - 滚动您要单击的按钮进入视图.多数民众赞成也可能是失败的原因.
//element is WebElement
(JavascriptExecutor)driver.executeScript("arguments[0].scrollIntoView(true);", element);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37404 次 |
| 最近记录: |