繁体   English   中英

org.openqa.selenium.NoSuchElementException:无法找到元素:

[英]org.openqa.selenium.NoSuchElementException: Unable to locate element:

码:

public void Test2() throws Exception{ 
Thread.sleep(5000); 
driver.findElement(By.id("cboMenu")).click(); 
driver.findElement(By.xpath(".//*[@id='cboMenu/option[3]")).click();
}

错误:

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

请使用显式等待而不是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();
}

接下来,确认您的按钮没有出现在其他iFrame中。 如果这样做,请将iFrame更改为您内部的元素:

driver.switchTo().frame("IFRAME_ID");

IFRAME_ID来自DOM:

<iframe id="IFRAME_ID">    

您可以更改旁边visibilityOfElementLocatedpresenceOfElementLocated ,将验证一个元素出现在DOM,但并不一定意味着该元素是可见的。 可以通过单击按钮来了解webDriver是否在正确的范围内,这是一个很好的线索。

附加提示-滚动您想要单击的按钮进入视图。 那也可能是失败的原因。

//element is WebElement    
(JavascriptExecutor)driver.executeScript("arguments[0].scrollIntoView(true);", element); 

这解决了我的问题:)

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.context("WEBVIEW_com.openstream.cueme.services.workbench");
        Thread.sleep(10000);
        driver.findElementById("userId").sendKeys("sysadmin");
        driver.findElementById("CuemePassword").sendKeys("MMNext13#");

试试下面的代码

public void Test2() throws Exception{
 Thread.sleep(5000);
 driver.findElement(By.id("cboMenu")).click();
 driver.findElement(By.xpath(".//*[@id='cboMenu']/option[3]")).click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM