I want to log into facebook, and then open a specific facebook url to get a list of people, working for a specific company (e.g. to get the employees of google, I need to go to https://www.facebook.com/search/str/google/pages-named/employees/present/intersect ) My first thought was to simply login (which works fine), and then go to the specific page using driver.navigate().to()
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com");
WebElement emailElement = driver.findElementById("email");
WebElement passwordElement = driver.findElementById("pass");
emailElement.sendKeys("[email protected]");
passwordElement.sendKeys("xxxx");
emailElement.submit();
driver.navigate().to("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");
However, this way, the facebook page is not available, and I’m prompted to log in again, even though the page is opened in the same browser tab?!
The second thought was to log in first, get the cookie, and then use is this cookie for a new driver:
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com");
WebElement emailElement = driver.findElementById("email");
WebElement passwordElement = driver.findElementById("pass");
emailElement.sendKeys("[email protected]");
passwordElement.sendKeys("xxxxx");
emailElement.submit();
Set<Cookie> cookies = driver.manage().getCookies();
FirefoxDriver driver2 = new FirefoxDriver();
for (Cookie cookie : cookies) {
Cookie cookieNew = new Cookie.Builder(cookie.getName(), cookie.getValue()).expiresOn(cookie.getExpiry())
.isHttpOnly(cookie.isHttpOnly()).isSecure(cookie.isSecure()).path(cookie.getPath()).build();
driver2.manage().addCookie(cookieNew);
}
driver2.get("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");
}
However, that way, an exception is thrown: org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse
What am I doing wrong?