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
原创粉丝点击