selenium学习(一):cookie的获取、登录、获取元素

摆脱了“心急吃不了热豆腐”的无头苍蝇状态之后,在正式开始认真研究爬虫之前,为了不至于没东西交差,先用selenium模仿着写了一个“类爬虫”。

为什么选择selenium:selenium是一款自动化的测试工具,可以模拟用户对浏览器的操作。这里想强调的是完全的模拟,你甚至可以把他想象为按键精灵。。。。。。。(勿喷,我是菜鸟),当然它还有很多很强大的功能,我暂时没有接触到。但是对于要写爬虫的我来说,最大的优势就是其绕过了“前端渲染”这种页面(即用ajax异步请求数据),因为这种页面用普通的爬虫是没办法扒到数据的,必须要用http请求来模拟ajax请求balabalabala...(正在学习模拟请求),或者是爬虫结合selenium这种自动化的测试工具一起上。

本次测试的网站为 -》www.pinterest.com  一个图片网站,应该为无限的瀑布流网站.....故肯定是采用的ajax请求。

另其登录页面 和 浏览界面的网址是不变的(即是在www.pinterest.com这个网站登录,同样也是在www.pinterest.com进行浏览。),所以需要获取cookie来进行操作。

先贴代码:

public class Test {



	public static void main(String[] args) throws InterruptedException {

		/*
		 * 不同浏览器的设置不同,推荐使用火狐浏览器。不推荐使用IE浏览器。
		 * 此处我用的火狐版本为40.4,selenium版本为2.53.1
		 */
		System.setProperty("webdriver.firefox.bin", "D:/火狐浏览器/firefox.exe");

		System.out.println("start");

		WebDriver driver = new FirefoxDriver();
		
		/*
		 * 由于要设置cookie,所以在设置之前必须就建立连接。
		 * 否则会报错。Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
		 * 故第一句get是不能少的。
		 */
		driver.get("https://www.pinterest.com/");
		
		for(Cookie cookie:Test.click("你的账号", "你的密码")){
			driver.manage().addCookie(cookie);
			
		}
		driver.manage().window().maximize();
		
		driver.get("https://www.pinterest.com/");
		
		/*
		 * 此处我使用set来去重,当然不是什么好办法。只是这个版本只是最初版
		 * 之后会再优化
		 */
		Set<String> result = new HashSet<String>();
		
		int i = 0;
		
		int num = 4600;
		
		while(true){
			System.out.println(i++);
			Thread.sleep(1000*20);  //拖动滚动条之后,休眠,便于网站加载数据
			List<WebElement> list = driver.findElements(By.cssSelector("img.pinImg.fullBleed.loaded.fade"));
			for(WebElement webElement:list){
				String url = webElement.getAttribute("src");
				System.out.println("url:"+url);
				result.add(url);
			}
			System.out.println("result.size = "+result.size());
			if(list.size() == 0){
				break;
			}
			
			int roll = num*i;
			System.out.println("roll:"+roll);
			
			/*
			 * 这个方法模拟向下拖动  垂直滚动条
			 */
			String setscroll = "document.documentElement.scrollTop=" + "" + roll; 
			JavascriptExecutor jse=(JavascriptExecutor) driver;
			jse.executeScript(setscroll); 
		}

		driver.close();

		System.out.println("end");

	}

	
	/**
	 * 获取cookie
	 * @param username
	 * @param password
	 * @return
	 * @throws InterruptedException
	 */
	public static Set<Cookie> click(String username,String password) throws InterruptedException{
		
		System.setProperty("webdriver.firefox.bin", "D:/火狐浏览器/firefox.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.pinterest.com/");
		//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//Thread.sleep(1000*8);
		driver.findElement(By.xpath(".//*[@id='userEmail']")).clear();
		driver.findElement(By.xpath(".//*[@id='userEmail']")).sendKeys(username);
		driver.findElement(By.xpath(".//*[@id='userPassword']")).clear();
		driver.findElement(By.xpath(".//*[@id='userPassword']")).sendKeys(password);
		driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/div/div/div[2]/div[3]/div[1]/div[3]/div/div[1]/div/div[2]/form/div/ul/div[1]/div[2]/li[2]/div/button")).click();

		Thread.sleep(1000*10);

		Set<Cookie> cookies = driver.manage().getCookies();
		
		System.out.println("Cookie.size = " + cookies.size());
		
		driver.close();

		return cookies;
	}

}


首先需要在这个网站注册一个账号。

在代码相应处填写你的账号和密码。

首先调用一个方法,获取cookie。

然后再利用cookie进行登录

在利用cookie的时候要注意这个错误,解决方法已经写在代码里了。

Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain

当然你直接在登录好的页面进行操作也可以。

之后就是你该干嘛干嘛。


缺点是效率很低下。


继续研究爬虫,随时分享进度~

欢迎各位指正交流。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值