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软件测试网0SH Z9j @'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
-fa!p RhB o+P\0 由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素51Testing软件测试网-e#?L(s%VyT
比如以下一段xml源文件:51Testing软件测试网4y E!it Z!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">
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软件测试网0SH Z9j @'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
-fa!p RhB o+P\0 由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素51Testing软件测试网-e#?L(s%VyT
比如以下一段xml源文件:51Testing软件测试网4y E!it Z!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">
<!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软件测试网.gJ7Wg
?m
51Testing软件测试网1o3B+[R.`(r4nU"j'QR
原本可以通过
D \,mYQ0WebElement element = driver.findElement(By.linkText("密码重置"));
et}V:M+GV3F0来定位此元素,但是由于该元素在iframe id="left_frame"这个frame里边 所以需要先通过定位frame然后再定位frame里边的某一个元素的方法定位此元素
;oXa1qd8R0E0WebElement element =driver.switchTo().frame("left_frame").findElement(By.linkText("密码重置"));51Testing软件测试网\kH*^xy!}2~_(S
51Testing软件测试网? k&u3c&@.u/}Ol2fT&?
3.不在同一个frame里边查找元素51Testing软件测试网%` d.eWT+o [I:Z7S
大家可能会遇到页面左边一栏属于left_frame,右侧属于right_frame的情况,此时如果当前处在51Testing软件测试网X eS?/p.x/{K@
left_frame,就无法通过id定位到right_frame的元素。此时需要通过以下语句切换到默认的content
q[r*X8\0driver.switchTo().defaultContent();
2b*tog(\(I"]-{0
*ilCX)a'ARe_0例如当前所在的frame为left_frame51Testing软件测试网$h1r4i-ouq0G$cL
/Z0N0JRgw"j)`NM(N-{0 WebElement xiaoshoumingxi_element = driver.switchTo().frame("left_frame").findElement(By.linkText("销售明细"));
1A8tz$N#BS8Q7R0 xiaoshoumingxi_element.click();51Testing软件测试网a z8X7{c]t.r
LLX4}~g ko|0需要切换到right_frame
g$i bzT}B0 driver.switchTo().defaultContent();
)lA~6_ WaI"g0 51Testing软件测试网*y3T4^!q3Q!@b'rv&o
Select quanzhong_select2 = new Select(driver.switchTo().frame("right_frame").findElement(By.id("coupon_type_str")));
51Testing软件测试网1o3B+[R.`(r4nU"j'QR
原本可以通过
D \,mYQ0WebElement element = driver.findElement(By.linkText("密码重置"));
et}V:M+GV3F0来定位此元素,但是由于该元素在iframe id="left_frame"这个frame里边 所以需要先通过定位frame然后再定位frame里边的某一个元素的方法定位此元素
;oXa1qd8R0E0WebElement element =driver.switchTo().frame("left_frame").findElement(By.linkText("密码重置"));51Testing软件测试网\kH*^xy!}2~_(S
51Testing软件测试网? k&u3c&@.u/}Ol2fT&?
3.不在同一个frame里边查找元素51Testing软件测试网%` d.eWT+o [I:Z7S
大家可能会遇到页面左边一栏属于left_frame,右侧属于right_frame的情况,此时如果当前处在51Testing软件测试网X eS?/p.x/{K@
left_frame,就无法通过id定位到right_frame的元素。此时需要通过以下语句切换到默认的content
q[r*X8\0driver.switchTo().defaultContent();
2b*tog(\(I"]-{0
*ilCX)a'ARe_0例如当前所在的frame为left_frame51Testing软件测试网$h1r4i-ouq0G$cL
/Z0N0JRgw"j)`NM(N-{0 WebElement xiaoshoumingxi_element = driver.switchTo().frame("left_frame").findElement(By.linkText("销售明细"));
1A8tz$N#BS8Q7R0 xiaoshoumingxi_element.click();51Testing软件测试网a z8X7{c]t.r
LLX4}~g ko|0需要切换到right_frame
g$i bzT}B0 driver.switchTo().defaultContent();
)lA~6_ WaI"g0 51Testing软件测试网*y3T4^!q3Q!@b'rv&o
Select quanzhong_select2 = new Select(driver.switchTo().frame("right_frame").findElement(By.id("coupon_type_str")));