转自http://www.51testing.com/html/87/300987-831171.htmlphp
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">html
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."); }
node