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/zhishuxiangqin 超链接打开新窗⼝的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 = WindowHandle();
Set<String> handles = WindowHandles();
for (String s : handles) {
if (s.equals(currentHandle))
continue;
else {
driver.switchTo().window(s);
if (Title().contains(windowTitle)) {
flag = true;
System.out.println("Switch to window: "
+ windowTitle + " successfully!");
break;
selenium获取cookie} 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();
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
for (Cookie ck : driver.manage().getCookies()) {
bw.Name() + ";" + ck.getValue() + ";"
+ ck.getDomain() + ";" + ck.getPath() + ";"
+ ck.getExpiry() + ";" + ck.isSecure());
}
} catch (IOException e) {
} 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((adLine())!= null) {
StringTokenizer str=new StringTokenizer(line,";");
while(str.hasMoreTokens())
{
String Token();
String Token();
String Token();
String Token();
Date expiry=null;
String dt;
if(!(Token()).equals(null))
{
//expiry=new Date(dt);
System.out.println();
}
boolean isSecure=new Token()).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();            }
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。