1.从字符串中提取数字
public String extractNumFromString(String str)
{
String str2="";
if(str != null && !"".equals(str)){
for(int i=0;i<str.length();i++){
if(str.charAt(i)>=48 && str.charAt(i)<=57)
{
str2+=str.charAt(i);
}
}
}
return str2;
}
2.验证json数据
public static String getJsonString(String strURL,String jsessionId) throws SocketTimeoutException,UnknownHostException,FileNotFoundException {
StringBuffer json = new StringBuffer();
HttpURLConnection conn = null;
BufferedReader br = null;
try {
URL url = new URL(strURL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cookie","JSESSIONID=" + jsessionId);
conn.setReadTimeout(30*1000);
InputStream is = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(is,"gb2312"));
String temp = null;
while((temp=br.readLine())!=null){
System.out.println("temp:"+temp);
json = json.append(temp);
}
System.out.println(conn.getHeaderField("Cookie"));;
}catch(SocketTimeoutException ste){
ste.printStackTrace();
}catch(UnknownHostException h){
h.printStackTrace();
}catch(FileNotFoundException fe){
fe.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
//conn.disconnect();
try {
if(br!=null){
br.close();
br = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
return json.toString();
}
3.滚动页面到指定位置(异步加载,找不到元素的情况使用)
public void scroll(String x, String y) {
if (x.equals("left")) {
x = "0";
} else if (x.equals("right")) {
x = "document.body.scrollWidth";
} else if (x.equals("middle")) {
x = "document.body.scrollWidth/2";
}
if (y.equals("top")) {
y = "0";
} else if (y.equals("buttom")) {
y = "document.body.scrollHeight";
} else if (y.equals("middle")) {
y = "document.body.scrollHeight/2";
}
((JavascriptExecutor)driver).executeScript(String.format("scroll(%s,%s);", x, y));
}
4.切换浏览器(按打开顺序)
public void switchBwoser(int num) {
Set<String> allHandles = driver.getWindowHandles();
String[] temp = new String[allHandles.size()];
int i = 0;
for(Iterator it = allHandles.iterator(); it.hasNext();i++){
temp[i] = (String)it.next();
}
driver.switchTo().window(temp[num-1]);
}
5.js操作select
例子:<select id="selector"></select>
a、设置value为pxx的项选中
$("#selector").val("pxx");
b、设置text为pxx的项选中
$("#selector").find("option[text='pxx']").attr("selected",true);
c、获取当前选中项的value
$("#selector").val();
d、获取当前选中项的text
$("#selector").find("option:selected").text();
浙公网安备 33010602011771号