org.openqa.selenium.NoSuchElementException:没有这样的元素
运行SeleniumWebDriver 2.37.1 我在运行测试时收到间歇性故障,并收到以下错误:org.openqa.selenium.NoSuchElementException:没有这样的元素,selenium,webdriver,nosuchelementexception,Selenium,Webdriver,Nosuchelementexception,运行SeleniumWebDriver 2.37.1 我在运行测试时收到间歇性故障,并收到以下错误: org.openqa.selenium.NoSuchElementException: no such element 有时考试会通过,但大多数时候会失败。以下是堆栈跟踪: org.openqa.selenium.NoSuchElementException:没有这样的元素 (会话信息:chrome=31.0.1650.57) (驾驶员信息: chromedriver=2.6.232923,平
org.openqa.selenium.NoSuchElementException: no such element
有时考试会通过,但大多数时候会失败。以下是堆栈跟踪:
org.openqa.selenium.NoSuchElementException:没有这样的元素
(会话信息:chrome=31.0.1650.57)
(驾驶员信息:
chromedriver=2.6.232923,平台=Windows NT 6.1 SP1 x86_64)
(警告:
服务器未提供任何堆栈跟踪信息)
命令持续时间或超时:10.04秒
有关此错误的文档,请访问:
http://seleniumhq.org/exceptions/no_such_element.html
构建信息:
版本:“2.34.0”,修订版:
“11cd0ef93615408e0b6b3bfa28defe125906461a”,时间:“2013-08-06
11:43:14'
系统信息:os.name:'Windows 7',os.arch:'amd64',
os.version:'6.1',java.version:'1.7.0_45'
会话ID:
2375dca8adea8bb6962b0c87861cdd59
驾驶员信息:
org.openqa.selenium.chrome.ChromeDriver
能力
下面是我执行测试的代码:
@Test
public void loginPage(){
chrome.get("http://thisisafakeurl.com");
//entering username
WebElement usernameField = chrome.findElement(By.cssSelector("input[type='text'][id='usernameInput']"));
usernameField.sendKeys("fakeusername");
//entering password
WebElement passwordField = chrome.findElement(By.cssSelector("input[id='passwordInput']"));
passwordField.sendKeys("fakepassword");
//clicking submit button
chrome.findElement(By.cssSelector("input#loginBtn.rightButton")).click();
chrome.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//asserting a css selector. Not really needed. Just thrown in to learn.
// WebElement blankProjectLocator = chrome.findElement(By.cssSelector("li.iconGridItem[data-id='5']"));
// Assert.assertEquals("5", blankProjectLocator.getAttribute("data-id"));
//Clicking the Blank Project button
Actions actions = new Actions(chrome);
WebElement item1 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[1]"));
WebElement item2 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[2]"));
WebElement item3 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/header/span"));
WebElement item4 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]"));
actions.moveToElement(item4)
.moveToElement(item1)
.moveToElement(item2)
.moveToElement(item3)
.click().build().perform();
chrome.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Dragging the Click Area component onto the canvas
WebElement listContainer = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
WebElement componentList = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
WebElement clickArea = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]"));
WebElement arrowHead = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]/div[1]"));
WebElement imageHolderClickArea = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]/div[1]/div"));
WebElement componentNameClickArea = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]/div[2]"));
WebElement canvas = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));
actions.moveToElement(listContainer)
.moveToElement(componentList)
.clickAndHold(clickArea)
.moveToElement(arrowHead)
.moveToElement(imageHolderClickArea)
.moveToElement(componentNameClickArea)
.moveToElement(canvas)
.release().perform();
chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Dragging the close button onto the canvas
WebElement listContainerClose = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
WebElement componentListClose = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
WebElement closeButton = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]"));
WebElement componentThumb = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]/div[1]"));
WebElement imageHolderCloseButton = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]/div[1]/div"));
WebElement componentNameCloseButton = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]/div[2]"));
WebElement canvas1 = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));
actions.clickAndHold(closeButton)
.moveToElement(listContainerClose)
.moveToElement(componentListClose)
.moveToElement(componentThumb)
.moveToElement(imageHolderCloseButton)
.moveToElement(componentNameCloseButton)
.moveToElement(canvas1)
.release().perform();
//WebElement xAxisClose = chrome.findElement(By.xpath("//*[@id=\"c358\"]"));
//xAxisClose.clear();
//xAxisClose.sendKeys("240");
//WebElement yAxisClose = chrome.findElement(By.xpath("//*[@id=\"c359\"]"));
//yAxisClose.clear();
//yAxisClose.sendKeys("5");
//Dragging the video component onto the canvas
WebElement listContainerVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
WebElement componentListVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
WebElement videoComponent = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[3]"));
WebElement componentThumbVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[3]/div[1]"));
WebElement componentNameVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[3]/div[2]"));
WebElement canvas2 = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));
actions.clickAndHold(videoComponent)
.moveToElement(listContainerVideo)
.moveToElement(componentListVideo)
.moveToElement(componentThumbVideo)
.moveToElement(componentNameVideo)
.moveToElement(canvas2)
.release().perform();
chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Dragging the Text component onto the canvas
WebElement listContainerText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
WebElement componentListText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
WebElement textComponent = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[4]"));
WebElement componentThumbText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[4]/div[1]"));
WebElement componentNameText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[4]/div[2]"));
WebElement canvas3 = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));
actions.clickAndHold(textComponent)
.moveToElement(listContainerText)
.moveToElement(componentListText)
.moveToElement(componentThumbText)
.moveToElement(componentNameText)
.moveToElement(canvas3)
.release().perform();
stacktrace中的可单击链接将我指向以下代码行:
WebElement listContainer = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
因此,在测试之前,可以单击空白项目
按钮
根据我的调查,我可能会遇到一些同步问题,因为它是间歇性的。我是否需要包装一个try/catch异常而不是超时等待?还是我把这些放在代码中不正确的地方
有人能帮忙或建议我哪里可能出错吗 我想你的考试有时会因为以下情况而失败
chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
只需在测试开始时放置一次超时代码,并删除所有其他实例
chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
正如Yuvaraj HK所提到的,在代码中只使用一次隐式等待就足够了。它会隐式等待您试图在代码中找到的每个元素
chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
但请尽量减少隐式等待时间,因为这可能会增加代码执行时间
在某些情况下,元素可能需要30秒以上才能可见,在这种情况下可以使用显式等待
WebDriverWait some_element = new WebDriverWait(driver,100);
some_element.until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_some_element")));
//do anything you want with some_element
我强烈建议使用overXPath即使使用了xpath,也可以尝试使用较短的xpath使用id不是访问元素的唯一方法。它的父级可能具有唯一的类名或其他属性,您可以使用它们来创建高效的
xpath
或css选择器
对。。。所以我设法解决了这里发生的事情。当我使用IntelliJ时,似乎我需要告诉它调用每个方法的顺序。因此,加入
@FixMethodOrder(methodsorter.NAME_升序)
并在第一个方法上加上“a”,然后在第二个方法上加上“b”(我将问题中粘贴的代码拆分为方法),依此类推,它按顺序运行方法,并对每个方法进行等待:
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
我在WindowsServer2003上测试IE7时遇到了同样的问题。我通过删除Internet Explorer增强的安全配置修复了它。在Server 2003上,您可以通过执行以下操作来修复它:
- 打开控制面板
- 打开“添加或删除程序”
- 打开“添加/删除Windows组件”(在左侧边栏上)
- 取消选中Internet Explorer增强的安全配置
- 单击下一步
- 坚持到底
- 重新启动Internet Explorer
你打开新窗户了吗?如果是,您需要使驱动程序切换到新窗口,我将测试以下代码:
String currentWindow = driver.getWindowHandle();// get handle of current window
Set<String> handles = driver.getWindowHandles();// get handle of all windows
Iterator<String> it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
continue;
}
driver = driver.switchTo().window(it.next());// switch to new window
//business action
//xxxxxxx
}
driver.switchTo().window(currentWindow);//switch back to original window
String currentWindow=driver.getWindowHandle();//获取当前窗口的句柄
设置句柄=驱动程序。getWindowHandles();//获取所有窗口的句柄
迭代器it=handles.Iterator();
while(it.hasNext()){
if(currentWindow==it.next()){
继续;
}
driver=driver.switchTo().window(it.next());//切换到新窗口
//商业行为
//xxxxxxx
}
driver.switchTo()窗口(currentWindow)//切换回原始窗口
这可能是浏览器的问题。我遇到了这个问题,并尝试了各种方法来解决它,但直到浏览器的缩放比例被固定到100%后才起作用。这听起来可能有点简单有趣,但对我来说很有效。查看WebDriverWait类:谢谢你的评论。即使在将xpath构造函数转换为CSSSelector之后,我仍然会收到相同的“No-soke-Element”错误。我认为问题在于没有使用try/catch方法,因为当我调试并尝试跳过时,我在调试器中收到以下消息:reflectVeCallable(org.junit.internal.runners.model),这似乎表明它需要一个一次性的try/catch。虽然我正在试图理解如何做到这一点。你确定你使用了正确的选择器,你能粘贴一部分html代码和堆栈跟踪吗?这是可拖动元素@Amith003的html代码-堆栈跟踪太大,无法在评论中发布。”- 单击区域 “您始终可以编辑问题以粘贴代码……无论如何,您可以尝试使用此CSS选择器到达单击区域
li.ui-draggable>div.f-feature-A
No,没有提到切换窗口。正如我将对您所讨论的用例使用窗口句柄方法一样。
String currentWindow = driver.getWindowHandle();// get handle of current window
Set<String> handles = driver.getWindowHandles();// get handle of all windows
Iterator<String> it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
continue;
}
driver = driver.switchTo().window(it.next());// switch to new window
//business action
//xxxxxxx
}
driver.switchTo().window(currentWindow);//switch back to original window