Java org.openqa.selenium.NoSuchElementException:无法定位元素:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27227538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:00:40  来源:igfitidea点击:

org.openqa.selenium.NoSuchElementException: Unable to locate element:

javaselenium

提问by Swapna D

Code:

代码:

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

Error:

错误:

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

回答by Rupesh Shinde

Try below code

试试下面的代码

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

回答by Johnny

Please use explicit wait instead of the Thread.sleep(5000), like in the next example. It will provide you much clearer error regarding what you experience.

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

Next, verify your button doesn't appear in different iFrame. If you do, change the iFrame to the one you element inside:

接下来,验证您的按钮没有出现在不同的 iFrame 中。如果这样做,请将 iFrame 更改为您内部的元素:

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

The IFRAME_ID is taken from the DOM:

IFRAME_ID 取自 DOM:

<iframe id="IFRAME_ID">    

You can next change visibilityOfElementLocatedto presenceOfElementLocated, that will verify that an element is present on the DOM but does not necessarily mean that the element is visible. It can be good clue to know if your webDriver is in the correct scope with the button you try to click.

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

Additional tip - scroll the button you want to click on into view. Thats could be also the reason to failure.

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

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

回答by Jayesh

this is solved my issue :)

这解决了我的问题:)

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#");