package com.demo.util;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public final class WebDriverManager {
private static WebDriver webDriver = null;
private WebDriverManager() {
super();
}
public static WebDriver getInstance() {
if(webDriver == null) {
System.setProperty("webdriver.chrome.driver","D:\\文件\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\Users\\CYH\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
synchronized (WebDriverManager.class){
if(webDriver == null) {
webDriver = new ChromeDriver(chromeOptions);
}
}
}
return webDriver;
}
}
package com.demo.demoweb;
import com.demo.util.WebDriverManager;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.MoveMouseAction;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
public class DemoWebApplicationTests {
private static WebDriver instance = null;
@BeforeClass
public static void init() {
instance = WebDriverManager.getInstance();
}
@Test
public void contextLoads() {
instance.manage().window().maximize();
instance.get("https://www.lagou.com");
try{
instance.findElement(By.linkText("全国站")).click();
}catch (Exception e){
e.printStackTrace();
}
instance.findElement(By.id("search_input")).sendKeys("淘宝美工");
instance.findElement(By.id("search_button")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance.findElement(By.linkText("成都")).click();
instance.findElement(By.linkText("3-5年")).click();
int i = 0;
List<Map<String,String>> list = new LinkedList<>();
do{
List<WebElement> elements = instance.findElements(By.cssSelector("#s_position_list > ul > li"));
Map<String,String> map = null;
for (WebElement element : elements) {
WebElement element2 = element.findElement(By.cssSelector("div.company_name > a"));
String text = element2.getText();
WebElement element1 = element.findElement(By.cssSelector("div.position a[href]"));
String href = element1.getAttribute("href");
String jobName = element1.findElement(By.tagName("h3")).getText();
String addrr = element1.findElement(By.cssSelector(".add")).getText();
String money = element.findElement(By.cssSelector(".money")).getText();
map = new HashMap<>(10);
map.put("companyName",text);
map.put("href",href);
map.put("addrr",addrr);
Integer ts=Arrays.asList(money.split("-")).stream().map(x->
Integer.valueOf(x.replaceAll("[k|K]", "000")
.replaceAll("万", "0000")))
.min(Comparable::compareTo).get();
map.put("money",String.valueOf(ts));
map.put("jobName",jobName);
list.add(map);
System.out.println(map);
}
String text = instance.findElement(By.cssSelector("span.pager_is_current")).getText();
System.out.println("当前页:"+text+"----------------------------------------------------------------");
//判断是否存在下一页 如果大于-1则不存在下一页
i = instance.findElement(By.cssSelector("#s_position_list span.pager_next")).getAttribute("class").indexOf("pager_next_disabled");
//下一页
instance.findElement(By.cssSelector("#s_position_list span.pager_next")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}while (i == -1);
Double money = list.stream().map(x -> Integer.valueOf(x.get("money"))).collect(Collectors.averagingDouble(x->x));
System.out.println("最低工资平均平均数:"+money);
/*for (Map<String, String> item : list) {
instance.get(item.get("href"));
item.put("detail",instance.findElement(By.cssSelector("div.job-detail")).getText());
item.put("addrr",instance.findElement(By.cssSelector("div.work_addr")).getText().replace("查看地图",""));
System.out.println(list);
}*/
}
@Test
public void zhilian(){
instance.manage().window().maximize();
instance.get("https://www.zhaopin.com");
try {
instance.findElement(By.cssSelector("div.risk-warning__content>button")).click();
}catch (Exception e){
System.out.println(e);
}
String windowHandle = instance.getWindowHandle();
instance.findElement(By.className("zp-search__input")).sendKeys("java");
instance.findElement(By.cssSelector(".zp-search__btn,.zp-search__btn--blue")).click();
Set<String> windowHandles = instance.getWindowHandles();
for (String handle : windowHandles) {
if(!windowHandle.equals(handle)){
instance = instance.switchTo().window(handle);
break;
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement element1 = instance.findElement(By.cssSelector("#search div.query-others__title__item.query-others__title__item"));
Actions action = new Actions(instance);
action.moveToElement(element1).perform();
instance.findElement(By.linkText("3-5年")).click();
instance.findElement(By.cssSelector("a.search-box__common__btn")).click();
List<Map<String,Object>> lit = new LinkedList<>();
Map<String,Object> map = null;
do{
WebElement next = instance.findElement(By.cssSelector("div.a-footer__top"));
action.moveToElement(next).perform();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<WebElement> listContent = instance.findElements(By.cssSelector("#listContent>div.contentpile__content__wrapper"));
for (WebElement item : listContent) {
String jobName = item.findElement(By.cssSelector("span.contentpile__content__wrapper__item__info__box__jobname__title")).getText();
WebElement element = item.findElement(By.cssSelector("div.contentpile__content__wrapper__item__info__box__cname>a"));
String companyTitle = element.getText();
WebElement content = item.findElement(By.cssSelector("div.contentpile__content__wrapper__item>a"));
String href = content.getAttribute("href");
String saray = item.findElement(By.cssSelector("p.contentpile__content__wrapper__item__info__box__job__saray")).getText();
System.out.println("岗位:"+jobName);
System.out.println("公司:"+companyTitle);
System.out.println("详情:"+href);
System.out.println("工资:"+saray);
System.out.println("##############################");
double max = Arrays.stream(saray.split("-"))
.filter(x -> x.replaceAll("[K|k]", "000").matches("\\d+"))
.mapToDouble(x -> Double.valueOf(x.replaceAll("[K|k]", "000"))).min().orElseGet(()->0);
if(max==0){
System.err.println("工资:"+saray);
continue;
}
map = new HashMap<>(4);
map.put("jobName",jobName);
map.put("companyTitle",companyTitle);
map.put("href",href);
map.put("saray",max);
lit.add(map);
}
String num = instance.findElement(By.cssSelector("#pagination_content span.soupager__index.soupager__index--active")).getText();
System.out.println("当前页:"+num+"-----------------------------------------------------------------------------");
//下一页
WebElement element = instance.findElements(By.cssSelector("#pagination_content > div > button")).get(1);
String attribute = element.getAttribute("disabled");
if("true".equals(attribute)){
break;
}
element.click();
}while (true);
OptionalDouble average = lit.stream().mapToDouble(x -> (double) x.get("saray")).average();
System.out.println("平均工资:"+average.getAsDouble());
}
}
智联