org.openqa.selenium.NoSuchElementException:

http://www.blogjava.net/qileilove/archive/2014/12/11/421309.html

selenium webdriver定位不到元素的五种原因及解决办法
   1.动态id定位不到元素
  for example:
  //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
  WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'写 信')]"));
  xiexin_element.click();
  上面一段代码注释掉的部分为通过id定位element的,但是此id“_mail_component_82_82”后面的数字会随着你每次登陆而变化,此时就无法通过id准确定位到element。
  所以推荐使用xpath的相对路径方法查找到该元素。

 2.iframe原因定位不到元素
  由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素
  比如以下一段xml源文件:
<iframe id="left_frame" scrolling="auto" frameborder="0" src="index.php?m=Index&a=Menu" name="left_frame" noresize="noresize" style="height: 100%;visibility: inherit; width: 100%;z-index: 1">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body class="menuBg">
<div id="menu_node_type_0">
<table width="193" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<tr>
<td id="c_1">
<table class="menuSub" cellspacing="0" cellpadding="0" border="0" align="center">
<tbody>
<tr class="sub_menu">
<td>
<a href="index.php?m=Coupon&a=SearchCouponInfo" target="right_frame">密码重置</a>
</td>
</tr>
  原本可以通过
  WebElement element = driver.findElement(By.linkText("密码重置"));
  来定位此元素,但是由于该元素在iframe id="left_frame"这个frame里边 所以需要先通过定位frame然后再定位frame里边的某一个元素的方法定位此元素
  WebElement element =driver.switchTo().frame("left_frame").findElement(By.linkText("密码重置"));
   3.不在同一个frame里边查找元素
  大家可能会遇到页面左边一栏属于left_frame,右侧属于right_frame的情况,此时如果当前处在
  left_frame,就无法通过id定位到right_frame的元素。此时需要通过以下语句切换到默认的content
  driver.switchTo().defaultContent();
  例如当前所在的frame为left_frame
  WebElement xiaoshoumingxi_element = driver.switchTo().frame("left_frame").findElement(By.linkText("销售明细"));
  xiaoshoumingxi_element.click();
  需要切换到right_frame
  driver.switchTo().defaultContent();
  Select quanzhong_select2 = new Select(driver.switchTo().frame("right_frame").findElement(By.id("coupon_type_str")));
  quanzhong_select2.selectByVisibleText("售后0小时");
   4. xpath描述错误
  这个是因为在描述路径的时候没有按照xpath的规则来写 造成找不到元素的情况出现
   5.点击速度过快 页面没有加载出来就需要点击页面上的元素
  这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来实现
  例如:
  //用WebDriverWait和until实现显示等待 等待欢迎页的图片出现再进行其他操作
  WebDriverWait wait = (new WebDriverWait(driver,10));
  wait.until(new ExpectedCondition<Boolean>(){
  public Boolean apply(WebDriver d){
  boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.xpath("//center/div[@class='welco']/img")).isDisplayed();
  return loadcomplete;
  }
  });
  也可以自己预估时间通过Thread.sleep(5000);//等待5秒 这个是强制线程休息
   6.firefox安全性强,不允许跨域调用出现报错
  错误描述:uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location:
  解决办法:
  这是因为firefox安全性强,不允许跨域调用。
  Firefox 要取消XMLHttpRequest的跨域限制的话,第一
  是从 about:config 里设置 signed.applets.codebase_principal_support = true; (地址栏输入about:config 即可进行firefox设置)
  第二就是在open的代码函数前加入类似如下的代码: try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { alert("Permission UniversalBrowserRead denied."); }
  最后看了乙醇的文章
import java.io.File;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
public classButtonDropdown {
public static voidmain(String[] args) throws InterruptedException {
WebDriver dr = newChromeDriver();
File file = newFile("src/button_dropdown.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("nowaccesss %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
// 定位text是watir-webdriver的下拉菜单
// 首先显示下拉菜单
dr.findElement(By.linkText("Info")).click();
(newWebDriverWait(dr, 10)).until(new ExpectedCondition<Boolean>(){
public Booleanapply(WebDriver d){
returnd.findElement(By.className("dropdown-menu")).isDisplayed();
}
});
// 通过ul再层级定位
dr.findElement(By.className("dropdown-menu")).findElement(By.linkText("watir-webdriver")).click();
Thread.sleep(1000);
System.out.println("browser will be close");
dr.quit();
}
}
  然后我自己定位的。
public XiaoyuanactivityPage zipaixiuye(){
driver.navigate().refresh();
luntan.click();
WebDriverWrapper.waitPageLoad(driver,3);
(new WebDriverWait(driver, 10)).until(newExpectedCondition<Boolean>() {
public Boolean apply(WebDriverdriver){
returndriver.findElement(By.className("TFB_sub_li")).isDisplayed();
}
});
driver.findElement(By.className("TFB_sub_li")).findElement(By.linkText("自拍秀")).click();
returnPageFactory.initElements(this.getDriver(),
XiaoyuanactivityPage.class);
}

转载于:https://www.cnblogs.com/dieyaxianju/p/5018696.html

情分析以下错误:Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"search-input"} Command duration or timeout: 3.97 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28' System info: host: 'LAPTOP-34K1M0P4', ip: '192.168.32.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=13.0.1, platform=XP, browserConnectionEnabled=true, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 450cd28c-219b-444e-a9ff-427428c704f0 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:326) at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:367) at org.openqa.selenium.By$ById.findElement(By.java:214) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:318) at shiyan2.crawler.main(crawler.java:18) Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"id","selector":"search-input"} Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28' System info: host: 'LAPTOP-34K1M0P4', ip: '192.168.32.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201' Driver info: driver.version: unknown at <anonymous class>.<anonymous method>(file:///C:/Users/Y/AppData/Local/Temp/anonymous3190348505889140058webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8904) at <anonymous class>.<anonymous method>(file:///C:/Users/Y/AppData/Local/Temp/anonymous3190348505889140058webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8913) at <anonymous class>.<anonymous method>(file:///C:/Users/Y/AppData/Local/Temp/anonymous3190348505889140058webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10878) at <anonymous class>.<anonymous method>(file:///C:/Users/Y/AppData/Local/Temp/anonymous3190348505889140058webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10883) at <anonymous class>.<anonymous method>(file:///C:/Users/Y/AppData/Local/Temp/anonymous3190348505889140058webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10825)
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值