selenium 常见的问题解决

问题之一:

例子:页面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();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值