private static ChromeDriver driver;
private static DevTools devTools;
static int count=0;
static {
System.setProperty("webdriver.chrome.driver", UrlConstant.chromedriver);
ChromeOptions chromeOptions = new ChromeOptions();
//chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
chromeOptions.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
//chromeOptions.setBinary("E:\\demo\\Application\\chrome.exe");
driver = new ChromeDriver(chromeOptions);
devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
try {
cfgChromeTool(driver);
} catch (Exception e) {
System.err.println("cfgChromeTool报错:");
}
}
/**
* watch the background request
* */
public static void cfgChromeTool(ChromeDriver driver){
try {
//Thread.sleep(10000);// 60秒
//driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
// Thread.sleep(10000);// 60秒
devTools.addListener(Network.responseReceived(), res -> {
String url = res.getResponse().getUrl();
if(url.contains(UrlConstant.url_search_count)){
try {
String responseBody = devTools.send(Network.getResponseBody(res.getRequestId())).getBody();
System.out.println("url----------"+url);
System.out.println("responseBody----------"+ JSON.parseObject(responseBody));
if(StringUtils.isNoneBlank(responseBody)&&responseBody.contains("nums")){
CountVo vo = JSONObject.parseObject(responseBody, CountVo.class);
count=vo.getNums();
}
} catch (Exception e) {
System.err.println("转responseBody报错:"+e);
}
}
});
} catch (Exception e) {
System.err.println("转responseBody报错:"+e);
}
}
public static void main(String[] args) throws Exception {
//BrowserUtil.invoke(BaseVo.builder().name("湘").build());
}
public static void invoke(BaseVo vo) throws Exception{
System.out.println(DateUtil.getDate()+"--------------开始------------------------"+vo+"-----------------------开始----------------------");
driver.get(UrlConstant.INDEX_URL);
operatePage(driver,vo);
}
/**
* to result page
* */
public static void operatePage(WebDriver driver, BaseVo vo) throws Exception {
Thread.sleep(3000);
//关闭弹窗提示
By tipABy = By.cssSelector("div.introjs-tooltipbuttons > a.introjs-button");
waitForLoad(driver, tipABy);
WebElement tipA = driver.findElement(tipABy);
tipA.click();
//点击 医疗器械 标题//直接页面url跳转,如果进首页需要这一步
//WebElement ylqxBtn = driver.findElement(By.xpath("//i[@class='el-icon-yiliaoqixie']"));
//ylqxBtn.click();
//可根据加载网速调整
//Thread.sleep(1000);
//点击 搜索框 填写搜索内容
By searchInputBy = By.cssSelector("div.search-input > input.el-input__inner");
waitForLoad(driver, searchInputBy);
WebElement searchInput = driver.findElement(searchInputBy);
searchInput.clear();
searchInput.sendKeys(vo.getName());
//searchInput.click();
//点击 搜索按钮
By searchBtnBy = By.cssSelector("div.el-input-group__append > button.el-button");
waitForLoad(driver, searchBtnBy);
WebElement searchBtn = driver.findElement(searchBtnBy);
searchBtn.click();
//切换标签页
// 获取所有windowHandle
Thread.sleep(1000);
Set<String> windowHandles = driver.getWindowHandles();
WebDriver.TargetLocator targetLocator = driver.switchTo();
String handle = "";
for (String windowHandle : windowHandles) {
handle = windowHandle;
}
// 切换到对应标签页
targetLocator.window(handle);
Thread.sleep(3000);//必须否则报错,原因不知道
//关闭第二页弹窗提示//introjs-tooltipbuttons
By tipABy2 = By.cssSelector("div.introjs-tooltipbuttons > a.introjs-button");//
waitForLoad(driver, tipABy2);
WebElement tipA2 = driver.findElement(tipABy2);
tipA2.click();
//点击 第二页 搜索按钮 获取后台请求 UrlConstant.url_search_page
//Thread.sleep(3000);//必须否则报错,原因不知道
/*By searchBtnBy2 = By.cssSelector("div.el-input-group__append > button.el-button > i.el-icon-search");//i.el-icon-search el-button--default
waitForLoad(driver, searchBtnBy2);
WebElement searchBtn2 = driver.findElement(searchBtnBy2);
searchBtn2.click();*/
//第一页表格数据
getTable();
Thread.sleep(1000);
//计算分页
By totalBy = By.cssSelector("span.el-pagination__total");//
waitForLoad(driver, totalBy);
WebElement totalBySpan = driver.findElement(totalBy);
String str = totalBySpan.getText();//共 16469 条
str=str.replace("共","");
str=str.replace("条","").trim();
count=Integer.parseInt(str);
System.out.println("总数count-----一共"+str+"条数");
int k = count/10;
int j = count%10;
if(j>0){
k=k+1;
}
System.out.println("----总页数-----"+k);
for (int l=2 ;l<=k; l++) {
clickPage(l);
Thread.sleep(2000);
getTable();
Thread.sleep(3000);
}
}
public static void clickPage(int pageNum){//当前点击页码pageNum
//分页
By pageBtn = By.cssSelector("ul.el-pager > li.number");//滑动验证按钮
waitForLoad(driver, pageBtn);
List<WebElement> select = driver.findElements(pageBtn);
System.out.println(DateUtil.getDate()+"-----clickNextPage——List的长度是:"+select.size());
for (WebElement page: select) {
if(String.valueOf(pageNum).equals(page.getText())){
page.click();
System.out.println(DateUtil.getDate()+"--------点击翻页按钮:页码是-------"+page.getText());
}
}
}
//获取页面数据
public static void getTable() throws InterruptedException {
//获取table内容
By tdText = By.cssSelector("div.cell");//div.cell > p
waitForLoad(driver, tdText);
List<WebElement> tds = driver.findElements(tdText);
StringBuffer bf =new StringBuffer();
int i= 1;
List<DataVo> l= Lists.newArrayList();
DataVo v=null;
for (WebElement td: tds) {
bf.append(td.getText());bf.append(",");
if(i%5==1){
v=new DataVo();
v.setDesc(td.getText());
System.out.println(i+"----1---"+td.getText()+"----"+JSON.toJSONString(v));
Thread.sleep(111);
}
i++;
if(i%5==2){
System.out.println(i+"----2---"+td.getText()+"----"+JSON.toJSONString(v));
v.setId(td.getText());
Thread.sleep(111);
}
if(i%5==3){
System.out.println(i+"----3---"+td.getText()+"----"+JSON.toJSONString(v));
v.setNum(td.getText());
Thread.sleep(111);
}
if(i%5==4){
System.out.println(i+"----4---"+td.getText()+"----"+JSON.toJSONString(v));
v.setRegName(td.getText());
Thread.sleep(111);
}
if(i%5==0){
System.out.println(i+"----5---"+td.getText()+"----"+JSON.toJSONString(v));
bf.append(" | ");
v.setProductName(td.getText());
l.add(v);
Thread.sleep(111);
}
}
System.out.println(bf);
exp(l);
}
/**
* waiting for element loading
* ***very important***
* if not do this can't operate web element
* */
public static void waitForLoad(final WebDriver driver, final By by) {
new WebDriverWait(driver, Duration.ofSeconds(60)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
WebElement element = driver.findElement(by);
if (element != null) {
return true;
}
return false;
}
});
}
public static Boolean exp(List<DataVo> data){
try {
//追加方式写入excel
File file = new File("/yjj"+DateUtil.getDateYmd()+".xlsx");
File tempFile = new File("/temp.xlsx");
if (file.exists()){
// 第二次按照原有格式,不需要表头,追加写入
EasyExcel.write(file,DataVo.class).needHead(false).
withTemplate(file).file(tempFile).sheet().doWrite(data);
}else {
// 第一次写入需要表头
EasyExcel.write(file,DataVo.class).sheet().doWrite(data);
}
if (tempFile.exists()){
file.delete();
tempFile.renameTo(file);
}
}catch (Exception e){
System.err.println("excel导出exp报错");
return false;
}
System.out.println("--------excel导出完成------总数: "+count);
return true;
}
本文转载自: https://blog.csdn.net/gzw876146161/article/details/134672707
版权归原作者 rose_marry 所有, 如有侵权,请联系我们删除。
版权归原作者 rose_marry 所有, 如有侵权,请联系我们删除。