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

1.动态id定位不到元素 51Testing软件测试网,~UQhn
for example:
y Z |\l n:|;R,n0        //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82")); 51Testing软件测试网$q,X)I6mw
        WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'写 信')]")); 51Testing软件测试网Gcwvr DH
        xiexin_element.click();
2u]_6S^0 51Testing软件测试网)_L,c)P ]%@EUH(`
   上面一段代码注释掉的部分为通过id定位element的,但是此id“_mail_component_82_82”后面的数字会随着你每次登陆而变化,此时就无法通过id准确定位到element。 51Testing软件测试网,o,Vz:a)pL-LU fl
   所以推荐使用xpath的相对路径方法查找到该元素。
5l"JZ0dj x-YP+T0
U}+Pj@^#?/ZfW0 2.iframe原因定位不到元素
?:Dw5| J7s*XS0 51Testing软件测试网k,} Y?pQm7`M
    由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素 51Testing软件测试网8p,V c8Xm}J~
比如以下一段xml源文件: 51Testing软件测试网r I,CIs*H r8r
<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>51Testing软件测试网#o(Xke8S0@|F#t

!`Bsdx@0原本可以通过
*n!Lv,q3bF)l0`V0WebElement element = driver.findElement(By.linkText("
密码重置"));
Nov{z {3TM X5|$R2OI;N0来定位此元素,但是由于该元素在
iframe id="left_frame"这个frame里边 所以需要先通过定位frame然后再定位frame里边的某一个元素的方法定位此元素51Testing软件测试网#C$`*]%l+d2n9Jg
WebElement element =driver.switchTo().frame("left_frame").findElement(
By.linkText("密码重置"));
} Dg'I:x\m~*K u051Testing软件测试网i;b7ba Ty
3.不在同一个frame里边查找元素51Testing软件测试网;gfvr s}-JE
大家可能会遇到页面左边一栏属于
left_frame,右侧属于right_frame的情况,此时如果当前处在51Testing软件测试网f,`&@g(AY#CLf^
left_frame,就无法通过id定位到right_frame的元素。此时需要通过以下语句切换到默认的content51Testing软件测试网4p,x|^rS+M$s5f[
driver.switchTo().defaultContent();
Z{5I8h8RlP9Z ?0
I+MX4~ NTZ'P/Yl0
例如当前所在的frame为left_frame
m7q!N0XEGl3^3z3b051Testing软件测试网+\h$n*Fymw:@C
       WebElement xiaoshoumingxi_element = driver.switchTo().frame("left_frame").findElement(By.linkText("销售明细"));51Testing软件测试网8Eu j i2oOlZ)ZU,\
       xiaoshoumingxi_element.click();51Testing软件测试网aaX*wV8^ H

i FaJ{#@}+}w z0
需要切换到right_frame     
zJ%[L.|a;l0       driver.switchTo().defaultContent();
9g1JNb1|keW0      
8`1c(rN&n(k0       Select quanzhong_select2 = new Select(driver.switchTo().frame("right_frame").findElement(By.id("coupon_type_str")));51Testing软件测试网x ~7~ fXi
       quanzhong_select2.selectByVisibleText("售后0小时");
2s!u-d$x5tq0

9UD {.DW3d `O3j2p04. xpath描述错误
pw[ F0LQNm0这个是因为在描述路径的时候没有按照xpath的规则来写 造成找不到元素的情况出现
z/}7P1Y:iN{6Pc0
"s sUx)G7M3m-S05.点击速度过快 页面没有加载出来就需要点击页面上的元素51Testing软件测试网 wcTi&jTT ?
这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来实现51Testing软件测试网0`*\yXb7H/s
例如:51Testing软件测试网)LnL&g4]5cmt
       //用WebDriverWait和until实现显示等待 等待欢迎页的图片出现再进行其他操作
K4hn0{/a0       WebDriverWait wait = (new WebDriverWait(driver,10));
m+J0~ e!W.HH1gN^0       wait.until(new ExpectedCondition<Boolean>(){
;H/Cl7yR7c~0           public Boolean apply(WebDriver d){51Testing软件测试网L;XY7q!i}d"{
               boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.xpath("//center/div[@class='welco']/img")).isDisplayed();51Testing软件测试网QOY&I_/Zr+G
               return loadcomplete;
+`)`:M_| N[`y-?/l0           }
`0eoSLU)d}0       });51Testing软件测试网N"y e t!H
也可以自己预估时间通过Thread.sleep(5000);//等待5秒 这个是强制线程休息
R#^yU?2H0
_ N A;JL~06.firefox安全性强,不允许跨域调用出现报错51Testing软件测试网Y7]a9T!@? |
错误描述:uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location:
y {"j,gXI051Testing软件测试网xOG%\m9~$_jB
解决办法:51Testing软件测试网k9N;^9Fea1m
这是因为firefox安全性强,不允许跨域调用。
;`k!Tzp&|0Firefox 要取消XMLHttpRequest的跨域限制的话,第一51Testing软件测试网 k t#dB6S$r;g'^/p
是从 about:config 里设置 signed.applets.codebase_principal_support = true; (地址栏输入about:config 即可进行firefox设置)51Testing软件测试网SYCd&X6?
第二就是在open的代码函数前加入类似如下的代码: try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { alert("Permission UniversalBrowserRead denied."); } 51Testing软件测试网 a`#A leYC} l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值