从头学习爬虫(十一)实战篇----模拟登入

本文主要实战用selenium模拟登入,用手动打码代替接打码平台。

package y;

import java.util.Scanner;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class SeleniumTest {
	private static Set<Cookie> cookies;
	 public static void login() {
		 	//驱动位置
	    	System.getProperties().setProperty("webdriver.chrome.driver","D:\\chromedriver_win32(1)\\chromedriver.exe");
	    	ChromeOptions options = new ChromeOptions();
	    	//浏览器位置
	    	options.setBinary("C:\\Program Files\\Chrome\\Chrone.exe");
	    	//System.getProperties().setProperty("webdriver.chrome.bin", ""); 
	        WebDriver driver = new ChromeDriver(options);
	        //打开百度登入页面
	        driver.get("https://passport.baidu.com/v2/?login&tpl=mn");
	        //切换用户名密码登入
	        driver.findElement(By.id("TANGRAM__PSP_3__footerULoginBtn")).click();
	        //输入账号
	        driver.findElement(By.id("TANGRAM__PSP_3__userName")).clear();
	        driver.findElement(By.id("TANGRAM__PSP_3__userName")).sendKeys("xxx");
	        //输入密码
	        driver.findElement(By.id("TANGRAM__PSP_3__password")).clear();
	        driver.findElement(By.id("TANGRAM__PSP_3__password")).sendKeys("xxx");
	        //人工输入验证码
	        Scanner sc= new Scanner(System.in);
	        String s=sc.nextLine();
	        driver.findElement(By.id("TANGRAM__PSP_3__verifyCode")).clear();
	        driver.findElement(By.id("TANGRAM__PSP_3__verifyCode")).sendKeys(s);
	        driver.findElement(By.id("TANGRAM__PSP_3__submit")).click();
	        //获取cookie信息*/
	        cookies = driver.manage().getCookies();
	        for (Cookie cookie : cookies) {
				System.out.println(cookie.getName()+":"+cookie.getValue());
			}
	        //后续把cookie 添加进header
	        driver.quit(); 
	    }
public static void main(String[] args)  {
	
	login();
	
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值