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);
}

org.openqa.selenium.NoSuchElementException:的更多相关文章

  1. org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element

    org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element(识别不到想要的元素) 想获取 ...

  2. 如果遇到找不到元素如何处理? Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"investmentframe"}

    常见几种原因与应对,详细参见http://www.blogjava.net/qileilove/archive/2014/12/11/421309.html 1,动态ID无法找到,用xpath路径解决 ...

  3. org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launch IE

    1.在启动ie浏览器前先加入属性设置一项: DesiredCapabilities ie = DesiredCapabilities.internetExplorer(); ie.setCapabil ...

  4. selenium:org.openqa.selenium.WebDriverException: f.QueryInterface is not a function

    今天用selenium2遇到问题 org.openqa.selenium.WebDriverException: f.QueryInterface is not a function 查了好久最后终于 ...

  5. Selenium2学习-041-chromedriver:org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status from unexpected alert open

    今天在写WebDriver处理弹出框(alert.confirm.prompt)演示实例脚本分发给朋友时,在其执行时未能成功执行,对应的部分错误详情如下: org.openqa.selenium.We ...

  6. appium运行报错.<init>(Lorg/openqa/selenium/remote/ErrorCodes;Z)V

    最近这几天就在学习appium,搭建环境就耗费了很多时间,不得不承认自己够笨的了,然后我把环境搭建好,写完脚本的时候,就报这个错了,当时是从某个群里直接下载的demo,不得不吐槽说,够坑的,是能跑通, ...

  7. org.openqa.selenium.StaleElementReferenceException

    org.openqa.selenium.StaleElementReferenceException如何解啊.什么原因造成的,貌似有时会出现,有时不会出现

  8. Selenium2学习-038-firefox、webdriver版本不对称问题解决:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055

    今天有个朋友在群里问,为何脚本运行不通过,其脚本操作步骤简单描述如下: 1.启动火狐浏览器 2.打开百度 3.查询框输入关键字 4.点击按钮[百度一下] 脚本挺简单的,其给出的应用报错信息如下所示: ...

  9. 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation

    解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...

随机推荐

  1. JavaScript学习二

    2019-05-30 15:08:24 加油,这几天在赶高数,都…… <!DOCTYPE html> <html> <head> <script type=& ...

  2. iTex导出PDF

    iText导出PDF,所需jar包如下: itext-asian-5.2.0.jar 支持导出中文的jar包 itextpdf-5.5.9.jar PDF核心jar包 bcprov-jdk15on-1 ...

  3. C# html生成PDF遇到的问题,从iTextSharp到wkhtmltopdf

    我们的网站业务会生成一个报告,用网页展示出来,要有生成pdf并下载的功能,关键是生成pdf. 用内容一段段去拼pdf,想想就很崩溃,所以就去网上找直接把html生成pdf的方法. 网上资料大部分都是用 ...

  4. 自学Python四 爬虫基础知识储备

    首先,推荐两个关于python爬虫不错的博客:Python爬虫入门教程专栏   和 Python爬虫学习系列教程 .写的都非常不错,我学习到了很多东西!在此,我就我看到的学到的进行总结一下! 爬虫就是 ...

  5. 5.23Java各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分

    作者:https://www.cnblogs.com/lyjin/p/6389349.html PO:持久对象(persistent object):---po就是在Object/Relation M ...

  6. IIS7部署网站的一些细节问题。

    1.不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况. 这个错误的原因是在 IIS 7中 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项.要取消锁定可以以 ...

  7. 多种效果进度指示层效果iOS源码项目

    该源码是一个多种效果进度指示层效果源码案例,源码KVNProgress,KVNProgress提供多种效果的进度指示层,有半透明效果,也支持全屏显示.指示层还高度支持自定义,可以按自己的需求定制.效果 ...

  8. 【sqli-labs】 less28a GET- Blind based -All you Union&Select Belong to us -String -Single quote-parenthesis(GET型基于盲注的去除了Union和Select的单引号带括号字符型注入)

    和less28没什么区别,直接上个payload吧 http://192.168.136.128/sqli-labs-master/Less-28a/?id=0')%a0uNion%a0sElect% ...

  9. 微信小程序开发常用方法

    1.函数中访问data中的数据 _this.setData({ // 日历数据 signList: dataList, // 当前日期 todayDay: str }) 2.if判断 wx:if=&q ...

  10. Python总结1

    时间:24日下午输入:input()输出:print()格式化输出通过某种占位符用于替换字符串中某个位置的字符.占位符:%s:可以替换任意类型%d:可以替换数字类型 需要掌握的#1.strip去左右两 ...