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

文章转载自: http://www.51testing.com/html/87/300987-831171.html

selenium webdriver定位不到元素的五种原因及解决办法的更多相关文章

  1. 转载:selenium webdriver定位不到元素的五种原因及解决办法

    1.动态id定位不到元素for example:        //WebElement xiexin_element = driver.findElement(By.id("_mail_c ...

  2. [Python爬虫] 之七:selenium webdriver定位不到元素的五种原因及解决办法(转载)

    转载:http://www.51testing.com/html/87/300987-831171.html 1.动态id定位不到元素for example:        //WebElement ...

  3. Selenium webdriver定位iframe里面元素两种方法

    以东方财富网登录页面为例: 在查找元素过程中,直接通过id或者xpath等找不到元素,查看页面源代码发现元素是属于iframe里,例如: <div class="wrap_login& ...

  4. Selenium webdriver定位iframe里面元素

    在查找元素过程中,直接通过id或者xpath等找不到元素,查看页面源代码发现元素是属于iframe里,例如: <div class="wrap_login"> < ...

  5. Selenium在定位的class含有空格的复合类的解决办法整理

    1.class属性唯一但是有空格,选择空格两边唯一的哪一个 <div id="tempConfigTable" class="dtb-style-1 table-d ...

  6. css inline元素和inline-block元素之间缝隙产生原因和解决办法

    行内元素产生水平空隙的原因及解决方案 这篇文章讲的很好,但是提供的解决方案没有这篇好实现 去除inline-block元素间间距的N种方法

  7. Python3 Selenium定位不到元素常见原因及解决办法

    Python3 Selenium定位不到元素常见原因及解决办法 一.问题描述 在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况: 报错信息: no such e ...

  8. Selenium WebDriver + Grid2 + RSpec之旅(五)---面向对象设计用例

    Selenium WebDriver + Grid2 + RSpec之旅(五) ----面向对象设计用例 前几节讲了怎么一步一步的从零开始到编写出一个简单的测试用例,这一节将要讲一下怎么让测试用例变得 ...

  9. 解决HTML5提出的新的元素不被IE6-8识别的解决办法

    解决HTML5提出的新的元素不被IE6-8识别的解决办法 <!--[if lt IE 9]> <script type="text/javascript" src ...

随机推荐

  1. js创建1-100的数组

    //实现方法一:循环赋值var arr1 = new Array(100);for(var i=0;i<arr1.length;i++){ arr1[i] = i;}console.log(ar ...

  2. ASP.NET Core学习系列

    .NET Core ASP.NET Core ASP.NET Core学习之一 入门简介 ASP.NET Core学习之二 菜鸟踩坑 ASP.NET Core学习之三 NLog日志 ASP.NET C ...

  3. noip2018游(AFO)记

    Day 0 到学车了,已经差不多四点了,领完一小袋比赛要用的就匆匆回了宾馆. 话说之前看地图的时候我们的宾馆最远,而且名字听起来并没有怎么高大上, 一看隔壁度豪大酒店就感觉应该比我们的酒店好.然鹅到了 ...

  4. ZOJ1008

    题目: ZOJ 1008 分析: 重排矩阵, 虽然题目给的时间很多, 但是要注意剪枝, 把相同的矩阵标记, 在搜索时可以起到剪枝效果. Code: #include <bits/stdc++.h ...

  5. 关于Integer比较问题

    public class Test { public static void main(String[] args) { Integer a=127; Integer b=127; System.ou ...

  6. Appium 测试微信小程序 Webview

    通过微信打开debugx5.qq.com,或者直接扫下面二维码   勾选[打开TBS内核Inspector调试功能]   Chrome查看页面元素 手机连接电脑,查看是否连接成功.如下展示设备号则为连 ...

  7. C# 百钱买百鸡

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  8. VS2013添加重构重命名功能

    VS2015.VS2017都自带重构重命名功能,早年用Eclipse的时候也频繁的使用这一功能,最近发现VS2013没有内置,需要装插件. 下载:https://marketplace.visuals ...

  9. jQuery的deferred对象解析

    参考: jQuery的deferred对象详解:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_defe ...

  10. CentOS openssh升级到openssh可用

    1. https://blog.csdn.net/moonpure/article/details/54575401 2. http://www.it165.net/admin/html/201303 ...