admin管理员组

文章数量:1594976

时间仓促,先把之前的测试代码贴上

/*
	 * 方案一 模拟搜索百度(打开百度,搜索,下三页,重新搜索,点击前三条任意一条
	 */
	public static void run_one(String search_one, String search_two, int mysqlid)
	{
		System.out.println(search_two);
		Map map = PhoneAgen.get_userAgent();
		//Map map = get_userAgent();
		
		String userAgent = (String) map.get("userAgent");
		
		DesiredCapabilities capabilities = null;
		capabilities = DesiredCapabilities.android();
		
		int phone_r_num = (int)map.get("phone_r_num");
		
		if(phone_r_num<=1)
		{
			capabilities.setBrowserName("iPhone");
			capabilities.setPlatform(Platform.MAC);
		}else
		{
			capabilities.setBrowserName("android");
			capabilities.setPlatform(Platform.ANDROID);
		}
		
//		System.out.println(capabilities.getBrowserName());
//		System.out.println(capabilities.getVersion());
//		System.out.println(capabilities.getPlatform());
		
	
		capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);
		
		
		PhantomJSDriver dr = new PhantomJSDriver(capabilities);
		dr.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
		
		try {
			
			Dimension POPULAR_DISPLAY_SIZE = new Dimension( (int)map.get("p_w"), (int) map.get("p_h"));
			dr.manage().window().setSize(POPULAR_DISPLAY_SIZE);
			System.out.println("打开百度");
			dr.get("http://m.baidu/");
			
			//((JavascriptExecutor) dr).executeScript("window.scrollBy(0, 700)"); 
			//System.out.println(dr.getPageSource());
			
			//Thread.sleep(3000);
			
			//snapshot((TakesScreenshot)dr, "1.png");
			
			WebElement searchBox = dr.findElement(By.id("index-kw"));
	        searchBox.sendKeys(search_one);
	        System.out.println("在搜索框中输入文字");
	        Thread.sleep(1000);
	        WebElement searchButton = dr.findElement(By.className("se-bn"));
	        //WebElement searchButton = dr.findElement(By.id("index-bn"));
	        searchButton.submit();;
	        System.out.println("搜索点击等待");
	        Thread.sleep(4000);
	        
	        //snapshot((TakesScreenshot)dr, "2.png");
			
			System.out.println("当前地址(第一页):"+dr.getCurrentUrl());
			
			
			
			
			//获取高度,用来模拟鼠标滚动
			String page_height = ((JavascriptExecutor) dr).executeScript("return document.body.scrollHeight").toString(); 
			
			int page_height_f  =  Integer.parseInt(page_height)-650;
			
			int mouse_scroll_num = mt_rand(4,5);
			for(int i=1;i<=mouse_scroll_num;i++)
			{
				int page_height_cur = (int) (page_height_f/mouse_scroll_num);
				int page_height_cur_n = mt_rand(0,50);
				if(mt_rand(1,2)==1)
				{
					page_height_cur -= page_height_cur_n;
				}else
				{
					page_height_cur += page_height_cur_n;
				}
				((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+page_height_cur+")");
				System.out.println("进入鼠标滚动(第一页)"+i+"--"+page_height_cur);
				Thread.sleep(mt_rand(1,3)*1000);
			}
			
			System.out.println("准备点击下一页");
			
			WebElement searchPage_one_next =  dr.findElement(By.className("new-nextpage-only")); //点击下一页 
			searchPage_one_next.click();
			
			//--------------------------------------------------------------
			
			Thread.sleep(3500);
			
			
			System.out.println("点击下一页后等待");
			
			//snapshot((TakesScreenshot)dr, "3.png");
			
			System.out.println("当前地址(第二页):"+dr.getCurrentUrl());
			
			String page_height2 = ((JavascriptExecutor) dr).executeScript("return document.body.scrollHeight").toString(); 
			
			int page_height_f2  =  Integer.parseInt(page_height2)-650;
			
			int mouse_scroll_num2 = mt_rand(4,5);
			for(int i=1;i<=mouse_scroll_num2;i++) 
			{
				int page_height_cur2 = (int) (page_height_f2/mouse_scroll_num2);
				int page_height_cur_n2 = mt_rand(0,50);
				if(mt_rand(1,2)==1)
				{
					page_height_cur2 -= page_height_cur_n2;
				}else
				{
					page_height_cur2 += page_height_cur_n2;
				}
				((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+page_height_cur2+")");
				System.out.println("进入鼠标滚动(第二页)"+i+"--"+page_height_cur2);
				Thread.sleep(mt_rand(1,3)*1000);
			}
			
			System.out.println("准备点击下一页");
			WebElement searchPage_two_next =  dr.findElement(By.className("new-nextpage")); //点击下一页 
			searchPage_two_next.click();
			
			
			//--------------------------------------------------------------
			
			Thread.sleep(3500);
			System.out.println("点击下一页后等待");
			
			System.out.println("当前地址(第三页):"+dr.getCurrentUrl());
			//snapshot((TakesScreenshot)dr, "4.png");
			Thread.sleep(2000);
			
			//重新搜索词
			WebElement searchPage_text =  dr.findElement(By.id("kw"));
			searchPage_text.clear();
		
			searchPage_text.sendKeys(search_two);
			Thread.sleep(1000);
			
			WebElement searchPage_se_bn =  dr.findElement(By.className("se-bn"));
			searchPage_se_bn.click();
			
			//--------------------------------------------------------------
			
			Thread.sleep(4000);
			System.out.println("重新搜索当前地址:"+dr.getCurrentUrl());
			//snapshot((TakesScreenshot)dr, "5.png");
			
			//成功增加记录
			String sql1="Update search Set num=num+1 Where id="+mysqlid;
			Dbhelper.executeNonQuery(sql1); 
			
			WebElement search_r1 = null;
			//随机点击前三个链接中的任意一个
			try{
				search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div[1]/div/div/div/a[1]"));
				int last_height = mt_rand(300,500);
				((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+last_height+")");
				System.out.println("找到广告,滚动"+last_height);
				
			}catch(Exception e)
			{
				int click_num = mt_rand(2,3); 
				search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div["+click_num+"]/div[1]/a"));
				
				System.out.println("没有广告,"+click_num);
			}
			
			search_r1.click();
			
			Thread.sleep(4000);
			System.out.println("打开搜索地址:"+dr.getCurrentUrl());
			//snapshot((TakesScreenshot)dr, "6.png");
			
			dr.quit();
			
			
			
		} catch (Exception e) {
			dr.quit();
			e.printStackTrace();
			
		}finally {
			dr.quit();
		}
	}

/*
	 * 方案三  搜索一次 过10-30随机秒 再次搜索
	 */
	
	public static void run_three(String search_one, String search_two, int mysqlid)
	{
		
		System.out.println(search_two);
		Map map = PhoneAgen.get_userAgent();
		
		String userAgent = (String) map.get("userAgent");
		
		DesiredCapabilities capabilities = null;
		capabilities = DesiredCapabilities.android();
		
		int phone_r_num = (int)map.get("phone_r_num");
		
		if(phone_r_num<=1)
		{
			capabilities.setBrowserName("iPhone");
			capabilities.setPlatform(Platform.MAC);
		}else
		{
			capabilities.setBrowserName("android");
			capabilities.setPlatform(Platform.ANDROID);
		}
		
//		System.out.println(capabilities.getBrowserName());
//		System.out.println(capabilities.getVersion());
//		System.out.println(capabilities.getPlatform());
		
	
		capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);
		
		
		PhantomJSDriver dr = new PhantomJSDriver(capabilities);
		dr.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
		
		try {
			
			Dimension POPULAR_DISPLAY_SIZE = new Dimension( (int)map.get("p_w"), (int) map.get("p_h"));
			dr.manage().window().setSize(POPULAR_DISPLAY_SIZE);
			System.out.println("打开百度");
			dr.get("http://m.baidu/");
			
			//((JavascriptExecutor) dr).executeScript("window.scrollBy(0, 700)"); 
			//System.out.println(dr.getPageSource());
			
			//Thread.sleep(3000);
			
			//snapshot((TakesScreenshot)dr, "1.png");
			
			WebElement searchBox = dr.findElement(By.id("index-kw"));
	        searchBox.sendKeys(search_one);
	        System.out.println("在搜索框中输入文字");
	        Thread.sleep(1000);
	        //By.xpath(xpathExpression)
	      //*[@id="index-bn"]
	        WebElement searchButton = dr.findElement(By.className("se-bn"));
	        searchButton.submit();;
	        System.out.println("搜索点击等待");
	        Thread.sleep(4000);
	        
	        //snapshot((TakesScreenshot)dr, "2.png");
			
			System.out.println("当前地址(第一页):"+dr.getCurrentUrl());
			
			//获取高度,用来模拟鼠标滚动
			String page_height = ((JavascriptExecutor) dr).executeScript("return document.body.scrollHeight").toString(); 
			
			int page_height_f  =  Integer.parseInt(page_height)-650;
			
			int mouse_scroll_num = mt_rand(4,5);
			for(int i=1;i<=mouse_scroll_num;i++)
			{
				int page_height_cur = (int) (page_height_f/mouse_scroll_num);
				int page_height_cur_n = mt_rand(0,50);
				if(mt_rand(1,2)==1)
				{
					page_height_cur -= page_height_cur_n;
				}else
				{
					page_height_cur += page_height_cur_n;
				}
				((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+page_height_cur+")");
				System.out.println("进入鼠标滚动(第一页)向下"+i+"--"+page_height_cur);
				Thread.sleep(mt_rand(1,3)*1000);
			}
			
			int mouse_scroll_num2 = mt_rand(4,5);
			for(int i=1;i<=mouse_scroll_num2;i++)
			{
				int page_height_cur = (int) (page_height_f/mouse_scroll_num);
				int page_height_cur_n = mt_rand(0,50);
				if(mt_rand(1,2)==1)
				{
					page_height_cur -= page_height_cur_n;
				}else
				{
					page_height_cur += page_height_cur_n;
				}
				((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+(-page_height_cur)+")");
				System.out.println("进入鼠标滚动(第一页)向上"+i+"--"+page_height_cur);
				Thread.sleep(mt_rand(1,3)*1000);
			}
			
			//重新搜索词
			WebElement searchPage_text =  dr.findElement(By.id("kw"));
			searchPage_text.clear();
		
			searchPage_text.sendKeys(search_two);
			Thread.sleep(1000);
			
			WebElement searchPage_se_bn =  dr.findElement(By.className("se-bn"));
			searchPage_se_bn.click();
			
			//--------------------------------------------------------------
			
			Thread.sleep(4000);
			System.out.println("重新搜索当前地址:"+dr.getCurrentUrl());
			//snapshot((TakesScreenshot)dr, "5.png");
			
			//成功增加记录
			String sql1="Update search Set num=num+1 Where id="+mysqlid;
			Dbhelper.executeNonQuery(sql1); 
			
			//随机点击前三个链接中的任意一个
			WebElement search_r1 = null;
			//随机点击前三个链接中的任意一个
			try{
				search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div[1]/div/div/div/a[1]"));
				int last_height = mt_rand(300,500);
				((JavascriptExecutor) dr).executeScript("window.scrollBy(0, "+last_height+")");
				System.out.println("找到广告,滚动"+last_height);
				
			}catch(Exception e)
			{
				int click_num = mt_rand(2,3); 
				search_r1 =dr.findElement(By.xpath("//*[@id=\"results\"]/div["+click_num+"]/div[1]/a"));
				
				System.out.println("没有广告,"+click_num);
			}
			search_r1.click();
		
			Thread.sleep(4000);
			System.out.println("打开搜索地址:"+dr.getCurrentUrl());
			//snapshot((TakesScreenshot)dr, "6.png");
			dr.quit();
			
			
		} catch (Exception e) {
			dr.quit();
			e.printStackTrace();
			
		}finally {
			dr.quit();
		}
	}


本文标签: 来路效果真实搜索引擎PhantomJS