selenium webdriver定位不到元素的五种原因及解决办法

上一篇 / 下一篇  2012-12-21 10:42:53 / 个人分类:Selenium

1.动态id定位不到元素
b(h9hh8h2y y ~0for example:51Testing软件测试网'Y6S%E6s^2m|"E
        //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
&^{;n4\;Gg0        WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'写 信')]"));51Testing软件测试网0S HZ9j @'gJ
        xiexin_element.click();51Testing软件测试网6D j{%_&W5}
51Testing软件测试网.b)E2~K:eU
   上面一段代码注释掉的部分为通过id定位element的,但是此id“_mail_component_82_82”后面的数字会随着你每次登陆而变化,此时就无法通过id准确定位到element。
;PT0f?!T0   所以推荐使用xpath的相对路径方法查找到该元素。51Testing软件测试网/YY*P;~ k VQ'vR

!S~S:H6P;f~02.iframe原因定位不到元素
"}#`J*^-w0
-f a!p RhB o+P\0    由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素51Testing软件测试网-e#?L(s%VyT
比如以下一段xml源文件:51Testing软件测试网4y E!itZ!L:L
<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">

ZT3\CD q*H04. xpath描述错误51Testing软件测试网Kg;_6b x4a `
这个是因为在描述路径的时候没有按照xpath的规则来写 造成找不到元素的情况出现51Testing软件测试网#~1p#t+s6yO-_+e;CQ

9F~2q*F7C-j05.点击速度过快 页面没有加载出来就需要点击页面上的元素
G&g|pl5g,d0这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来实现51Testing软件测试网-i7Z}M&l-O:rM-t
例如:51Testing软件测试网"wI"[Wd)q`|(BJ
       //用WebDriverWait和until实现显示等待 等待欢迎页的图片出现再进行其他操作
.J9dDc#n L*Xk0       WebDriverWait wait = (new WebDriverWait(driver,10));
&`lO3b-d$o0       wait.until(new ExpectedCondition<Boolean>(){51Testing软件测试网B y~)Q1s-rT5S%H;\u)C
           public Boolean apply(WebDriver d){51Testing软件测试网W;Hs7S:c(n;H-w;C
               boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.xpath("//center/div[@class='welco']/img")).isDisplayed();
Jbj(C:WT#x0               return loadcomplete;51Testing软件测试网 v!v s]3cF.xH/f&U
           }
|bM'W"?Bu)T0       });51Testing软件测试网 pOV(m,UA
也可以自己预估时间通过Thread.sleep(5000);//等待5秒 这个是强制线程休息
#Km~ |H3b051Testing软件测试网9gP0h{D)O S
6.firefox安全性强,不允许跨域调用出现报错51Testing软件测试网+c/x%TAi
错误描述:uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location:
{!a_XJ051Testing软件测试网-p)i$o\&C'Z P2I
解决办法:51Testing软件测试网3S'Ti"J h6A6Z
这是因为firefox安全性强,不允许跨域调用。 51Testing软件测试网$\x3e/Z,w ]RTQW
Firefox 要取消XMLHttpRequest的跨域限制的话,第一
u|M{5y%Y:\-l0是从 about:config 里设置 signed.applets.codebase_principal_support = true; (地址栏输入about:config 即可进行firefox设置)51Testing软件测试网)K5@?%s)T*R
第二就是在open的代码函数前加入类似如下的代码: try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { alert("Permission UniversalBrowserRead denied."); } 51Testing软件测试网 uPcgnVu`

TAG:

引用 删除 18201153313   /   2016-09-02 10:08:19
赞一个,很棒
引用 删除 18201153313   /   2016-09-02 10:08:19
赞一个,很棒
引用 删除 18201153313   /   2016-09-02 10:08:18
赞一个,很棒
引用 删除 18201153313   /   2016-09-02 10:08:18
赞一个,很棒
引用 删除 18201153313   /   2016-09-02 10:08:17
赞一个,很棒
引用 删除 18201153313   /   2016-09-02 10:08:15
赞一个,很棒
chenmo604的个人空间 引用 删除 chenmo604   /   2013-11-06 18:37:09
victory:
引用 删除 mhuang   /   2013-07-19 17:17:22
5
Mr.南柯 引用 删除 bob123654   /   2012-12-21 13:17:17
原帖由hebxtgsl于2012-12-21 13:14:01发表
顶一个
hebxtgsl的个人空间 引用 删除 hebxtgsl   /   2012-12-21 13:14:08
1
hebxtgsl的个人空间 引用 删除 hebxtgsl   /   2012-12-21 13:14:01
顶一个
 

评分:0

我来说两句

Open Toolbar