selenium 常见的问题解决
来源:互联网 发布:十堰卡五星软件 编辑:程序博客网 时间:2024/05/17 04:57
问题之一:
例子:页面html代码
<button class="buy-next btn buy-next-basic" type="submit" ng-disabled="BasicForm.$invalid">下一步</button>
定位方式:
driver.findElements(By.className("buy-next btn buy-next-basic" type="submit"))
问题:Compound class names not permitted,比如:
原因:HTML 元素赋予多个 class,例子中button元素有3个class(1、buy-next 2、btn 3、buy-next-basic)
解决方式:
使用xpath定位
driver.findElements(By.xpath("//button[@class='buy-next btn buy-next-basic']"));
问题之二:
selenium 当前窗口重新打开新窗口定位元素
例子:html页面代码
当前窗口的html代码:
<a ng-show="page.data.specialList[0].financeProductList[0].fundType!='04'" class="btn product-buy white font-w3" target="_blank" href="jijin/zhishuxiangqing.html?proId=620003">立即抢购</a>
超链接打开新窗口的html代码:
<a data-code="620003" data-type="02" buy-fund="" class="ui-btn">立即购买</a>
问题:在当前窗口定位新窗口的元素会找不到
解决方式:
使用selenium切换窗口,代码如下:
public static boolean switchToWindow(WebDriver driver,String windowTitle){ boolean flag = false; try { String currentHandle = driver.getWindowHandle(); Set<String> handles = driver.getWindowHandles(); for (String s : handles) { if (s.equals(currentHandle)) continue; else { driver.switchTo().window(s); if (driver.getTitle().contains(windowTitle)) { flag = true; System.out.println("Switch to window: " + windowTitle + " successfully!"); break; } else continue; } } } catch (NoSuchWindowException e) { System.out.print("Window: " + windowTitle + " cound not found!"); flag = false; } return flag; }
问题之三:
问题:在做web自动化时,登录的图形验证码通不过,必须人工去干预
解决的方法有三种:
1、一种是设置万能码
2、使用OCR破解
3、第一次登录后,获取cookie,并存储在本地。下次登录可以绕过登录页面,使用cookie登录
解决方案:
我使用的是cookie登录,原因是设置万能码需要修改代码,这样不好的地方是代码有可能直接发布到生成环境,OCR目前对于干扰线复杂的破解率低。
代码:
/** * 用户登录,创建cookie,用于绕过图形验证码 * @param driver */ public static void createCookie(WebDriver driver,File file) { FileWriter fw = null; BufferedWriter bw = null; try { // delete file if exists file.delete(); file.createNewFile(); fw = new FileWriter(file); bw = new BufferedWriter(fw); for (Cookie ck : driver.manage().getCookies()) { bw.write(ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure()); bw.newLine(); } } catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } finally { try { if(bw != null) { bw.flush(); bw.close(); } if(fw != null) { fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void sendCookie(WebDriver driver,File file) { FileReader fr= null; BufferedReader br= null; String line; try { fr = new FileReader(file); br = new BufferedReader(fr); while((line=br.readLine())!= null) { StringTokenizer str=new StringTokenizer(line,";"); while(str.hasMoreTokens()) { String name=str.nextToken(); String value=str.nextToken(); String domain=str.nextToken(); String path=str.nextToken(); Date expiry=null; String dt; if(!(dt=str.nextToken()).equals(null)) { //expiry=new Date(dt); System.out.println(); } boolean isSecure=new Boolean(str.nextToken()).booleanValue(); Cookie ck=new Cookie(name,value,domain,path,expiry,isSecure); driver.manage().addCookie(ck); } } } catch (IOException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { try { if(br != null) { br.close(); } if(fr != null) { fr.close(); } } catch (IOException e) { e.printStackTrace(); } } }
0 0
- selenium 常见的问题解决
- selenium的常见异常
- selenium常见错误的解决思路
- 常见电脑问题解决的方法
- Entity Framework 常见的问题解决
- 常见的性能问题解决方法
- selenium webdriver问题解决
- python的selenium的带https安全隐私问题解决方案
- python爬虫,selenium使用,Firefox-chrome-IE问题解决方法,selenium的基本操作
- VS2003 转 VS2005 常见的问题解决
- ubuntu中常见的中文问题解决方法
- Entity Framework 4.1常见的问题解决
- 我的 VC++ 常见错误问题解决办法
- flask-mail常见的邮箱配置问题解决
- selenium常见函数一
- selenium常见错误
- selenium元素定位及常见的表单元素定位
- 自动化测试工具selenium常见的元素定位方法
- X系统
- Html5将颠覆web前端创造机遇
- ulua--不规则按钮的响应区域
- 牛刀小试
- M4A格式的ID3信息提取
- selenium 常见的问题解决
- 怎么查看linux服务器的cpu信息和核心数?
- java虚拟机-垃圾回收
- ViewFlipper控件入门
- 操作系统页面置换算法之最近最少使用算法(LRU)
- Java基础课程-面向对象
- Eclipse快捷键 10个最有用的快捷键
- MariaDB是未来的开源方向
- 优先队列与堆排序