1. Selenium+动态id
有时候页面元素ID是动态递增的,比如__mbox-btn-1-content, __mbox-btn-3-content, __mbox-btn-5-content
String s1 = "__mbox-btn-";
String s2 = Integer.toString(i);
String s3 = "-content";
String s = s1 + s2 + s3;
WebElement LockConfirmBtn =
driver.findElement(By.xpath("//*[@id=" + "'" + s + "'" + "]"));
2. Selenium+表格寻找某条记录是否存在
WebElement tbody = webDriver.findElement(By.xpath("//*[@id=\"innerDivpackInfo\"]/table/tbody/tr[1]/td/table/tbody"));
boolean find = false;
String installWasVersion = generateInstallWasVersion(wasVersion).toUpperCase();
logger.debug(installWasVersion);
for(WebElement tr : tbody.findElements(By.tagName("tr"))){
logger.debug(tr.findElement(By.tagName("td")).getText().toUpperCase());
if(tr.findElement(By.tagName("td")).getText().toUpperCase().contains(installWasVersion)){
tr.findElement(By.cssSelector("input[type='radio']")).click();
find = true;
}
}
3. Selenium + windown upload file window
public static void openExe(String s) {
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(s);
Thread.sleep(3000);
} catch (Exception e) {
System.out.println("Error exec!");
}
}
主函数调用openExe(uploadPath);
4. Selenium + Log On
if (pageTitle.contains("Log On")) {
WebElement loginUserName = driver.findElement(By.xpath("//*[@id='j_username']"));
loginUserName.sendKeys(paraInfo.userName);
Thread.sleep(3000);
WebElement loginPassworkd = driver.findElement(By.xpath("//*[@id='j_password']"));
loginPassworkd.sendKeys(paraInfo.passwd);
Thread.sleep(3000);
WebElement loginBtn = driver.findElement(By.xpath("//*[@id='logOnFormSubmit']"));
loginBtn.click();
}