我正在尝试使用Selenium打开chrome用户,因此我仍然可以在该个人资料中使用会话和cookie。我已经阅读了多个线程,但是由于某些原因,这些线程中给出的解决方案无法为我解决问题。这是我的爬虫类:
WebDriver driver;
public TestCrawler(String driverPath){
System.setProperty("webdriver.chrome.driver",driverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/root/AppData/Local/Google/Chrome/User Data/Default");
ChromeDriverService chSvc = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("./chromedriver.exe")).usingAnyFreePort().build();
driver = new ChromeDriver(chSvc,options);
}
public void goGoogle(){
try{
driver.get("https://mail.google.com/mail/u/7/?tab=wm&ogbl#inbox");
WebElement username = driver.findElement(By.className("whsOnd"));
username.sendKeys("user");
WebElement usernameButton = driver.findElement(By.className("RveJvd"));
usernameButton.click();
synchronized (driver)
{
driver.wait(2000);
}
WebElement password = driver.findElement(By.className("whsOnd"));
password.sendKeys("pass");
synchronized (driver)
{
driver.wait(2000);
}
WebElement passwordButton = driver.findElement(By.className("RveJvd"));
passwordButton.click();
}
catch(Exception e){
System.out.println(e);
}