1

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?

2 Answers 2

2

I don't think it is a mandatory to store cookies to to get a list of people, working for a specific company. The following code block works well at my side :

driver.findElement(By.cssSelector("input[value='Log In']")).click();
driver.get("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");
System.out.println(driver.getTitle());
driver.quit();

Console Output :

Facebook Search
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your suggestion worked! However, i changed driver.findElement(By.cssSelector("input[value='Log In']")).click(); to driver.findElementById("loginbutton").click(); as the value of the input is depending on the language
0

Apply First Thought:

Setting up geckodriver for Selenium Java resolves the issue I think.

It needs to set geckodriver path with FirefoxDriver as below code:

System.setProperty("webdriver.gecko.driver", "PATH/TO/geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();

After submit() you need to use wait functionality to refresh the page like

emailElement.submit();      
//Use WebDriver wait.until() or sleep() to finish the submit action
driver.navigate().to("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.