使用selenium遍历frame中的表单信息 ;

时间:2022-02-04 05:34:22

遍历frame中的表单 :

 package webDriverPro;

 import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; import commUtil.Log;
import commUtil.OpenBrowserInfo; public class Demo2 {
public WebDriver driver; // 判断手机号码的合法性;
public static boolean regexp(String iphoneNumber) {
String regExp = "^[1]([3][0-9]{1}|59|58|88|89|52|30|87)[0-9]{8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(iphoneNumber);
return m.find();
} public Demo2(WebDriver driver) {
this.driver = driver;
} // 登陆
public void login() {
WebElement username = (new WebDriverWait(driver, 1000).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("username"));
}
}));
username.sendKeys("admin");
Log.info("----------------------------输入用密码-------------------------------------");
WebElement password = (new WebDriverWait(driver, 1000).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("password"));
}
}));
password.sendKeys("admin");
Log.info("----------------------------点击登录按钮-------------------------------------");
WebElement loginbutton = (new WebDriverWait(driver, 1000).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("html/body/div/form/div[2]/button"));
}
}));
loginbutton.click(); } // 点击系统管理和用户管理
public void ClicksysUserManager() {
Log.info("----------------------------点击系统管理-------------------------------------");
WebElement systemManage = (new WebDriverWait(driver, 1000).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("html/body/section/div[1]/ul/li[5]/a"));
}
}));
systemManage.click();
;
Log.info("----------------------------点击用户管理-------------------------------------"); WebElement usermanage = (new WebDriverWait(driver, 1000).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("html/body/section/div[1]/ul/li[5]/ul/li[1]/a"));
}
}));
usermanage.click();
driver.manage().window().maximize(); } // 遍历表单中所有内容
public void table1() {
// 查找frame
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
System.out.println("页面上共找到了" + iframes.size() + " 个iframe对象 !!");
WebElement frame = iframes.get(0);
// 进入frame
driver.switchTo().frame(frame);
// 共计条数和页数对象
String sumpage = driver.findElement(By.xpath("html/body/div/div/div/section/div/form/div[2]/li[7]/span"))
.getText();
System.out.println("===========================" + sumpage + "共计多少页------------------------------------");
String totalCount = (sumpage.substring(sumpage.indexOf("共") + 1, sumpage.lastIndexOf("条")));
int totalRecord = Integer.parseInt(totalCount.trim());
int pageSize = 10;
int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
System.out.println(
"---------------------------------总共有 :" + totalPageNum + "页 !---------------------------------------");
// 下一页对象;
WebElement nextPage;
// 遍历表单
for (int k = 1; k <= totalPageNum; k++) {
Log.info("当前是第" + k + " 页!");
nextPage = driver.findElement(By.xpath("html/body/div/div/div/section/div/form/div[2]/li[6]/a"));
List<WebElement> tables = driver.findElements(By.tagName("table"));
System.out.println("iframes *找到了 " + tables.size() + " 个tables !");
WebElement table = tables.get(0);
List<WebElement> rows = table.findElements(By.tagName("tr"));
for (WebElement row : rows) {
List<WebElement> cols = row.findElements(By.tagName("td"));
for (WebElement col : cols) {
System.out.print("|");
System.out.print(col.getText() + " |");
}
System.out.println("");
}
// 判断当前是第几页,如果是最后一页不执行点击下一页
if (k == totalPageNum) {
break;
} else {
nextPage.click();
System.out.println("点击次数=" + k + " ;");
}
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
OpenBrowserInfo.ThreadSleep();
} // return null;
} // 判断手机号码是否合规
public void iphoneNumberIsTrue() { // 查找frame
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
System.out.println("页面上共找到了" + iframes.size() + " 个iframe对象 !!");
WebElement frame = iframes.get(0);
// 进入frame
driver.switchTo().frame(frame);
// 共计条数和页数对象
String sumpage = driver.findElement(By.xpath("html/body/div/div/div/section/div/form/div[2]/li[7]/span"))
.getText();
System.out.println("===========================" + sumpage + "共计多少页------------------------------------");
String totalCount = (sumpage.substring(sumpage.indexOf("共") + 1, sumpage.lastIndexOf("条")));
int totalRecord = Integer.parseInt(totalCount.trim());
int pageSize = 10;
int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
System.out.println(
"---------------------------------总共有 :" + totalPageNum + "页 !---------------------------------------");
// 下一页对象;
WebElement nextPage;
// 遍历表单
for (int k = 1; k <= totalPageNum; k++) {
Log.info("当前是第" + k + " 页!");
nextPage = driver.findElement(By.xpath("html/body/div/div/div/section/div/form/div[2]/li[6]/a"));
List<WebElement> tables = driver.findElements(By.tagName("table"));
System.out.println("iframes *找到了 " + tables.size() + " 个tables !");
WebElement table = tables.get(0);
List<WebElement> rows = table.findElements(By.tagName("tr"));
for (int i = 0; i < rows.size(); i++) {
List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));
for (int j = 0; j < cols.size(); j++) {
// 获取手机号并判断手机号的合法性;
if (5 == j) {
String iphoneNumber = cols.get(j).getText();
if (!Demo2.regexp(iphoneNumber)) {
System.out.println(iphoneNumber + "-- 不合法的手机号 !");
}
} }
}
// 判断当前是第几页,如果是最后一页不执行点击下一页
if (k == totalPageNum) {
break;
} else {
nextPage.click();
System.out.println("点击次数=" + k + " ;");
}
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
OpenBrowserInfo.ThreadSleep();
}
} public static void main(String[] args) {
OpenBrowserInfo.fireFoxDriver();
WebDriver driver = new FirefoxDriver();
driver.get("http://139.196.180.205:8099/orient_new/");
Demo2 demo2 = new Demo2(driver);
OpenBrowserInfo.ThreadSleep();
demo2.login();
demo2.ClicksysUserManager();
demo2.table1();
//demo2.iphoneNumberIsTrue();
driver.close(); } }

运行结果:

相关文章