selenium找不到元素
1.页面元素处于不显示状态时,找不元素。必须使元素处于显示状态。使用js 或者 元素的点击事件等方式可以实现。
" src="index.php?m=Index&a=Menu" name="left_frame" noresize="noresize" style="height: 100%;visibility: inherit; width: 100%;z-index: 1">
原本可以通过
WebElement element = driver.findElement(By.linkText("密码重置"));
来定位此元素,但是由于该元素在iframe id="left_frame"这个frame里边 所以需要先通过定位frame然后再定位frame里边的某一个元素的方法定位此元素
WebElement element =driver.switchTo().frame("left_frame").findElement(By.linkText("密码重置"));
4.不在同一个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小时");
5. xpath描述错误
这个是因为在描述路径的时候没有按照xpath的规则来写 造成找不到元素的情况出现
6.点击速度过快 页面没有加载出来就需要点击页面上的元素
这个需要增加一定等待时间,显示等待时间可以通过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秒 这个是强制线程休息
WaitForPageLoad 也是可以的。
7.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."); }
最新文章
- webapi5
- mtr命令详解
- 14.Xcode8imageview图片圆角不显示的bug
- JS获取当前页面的URL信息
- BZOJ4530: [Bjoi2014]大融合
- 获取本地IP,并设置到IP控件
- ip_forward
- Java Executor并发框架(三)ThreadPoolExecutor 队列缓存策略
- jQuery mouseover与mouseenter的区别
- JDBC加载过程
- IE6滤镜在实战测试中能让父层里面的子元素产生阴影
- mysql的备份脚本
- Spring的核心模块解析
- Linux主机操作系统加固规范
- vue $refs的基本用法
- composer lavarel 安装
- 捕获海康威视IPCamera图像,转成OpenCV能够处理的图像(二)
- LeetCode122.买卖股票的最佳时机II
- 关于idea通过smalidea无源调试apk
- pager-taglib分页处理的使用
热门文章
- Unity 动画系统 AnimationEvent 动画事件
- Kibana6.x.x——启动后警告信息:Session cookies will be transmitted over insecure connections. This is not recommended.
- lintcode - 房屋染色
- hdu3746 KMP-next数组的应用
- 测试转型之路--学习ing
- mysql 示例数据库安装
- Spring配置问题:The prefix ";util"; for element ";util:map"; is not bound.
- Problem08 输入数字求和
- C实现shell管理的一个例子
- input类型为number去除上下按钮浏览器兼容方法