2

I a have small scraper for Facebook that uses chromedriver. However every time I login after a couple of seconds I have to click some button to allow Facebook notifications. Is there a way to do it automatically?

I've seen some suggest to press tab three times and then enter but I don't know when exactly I'll be prompted by the request and I don't want to press stuff at the wrong time.

Is there anything that can be one when creating the webdriver?

Bob Sacamano
  • 699
  • 15
  • 39

1 Answers1

4

Most likely, this is because every time you start a new ChromeDriver instance you get an entirely new Chrome profile, just like if you opened a new private browsing session. So all the client specific settings that the page you want to test has defined via cookies will be lost and have to be set again. (The main reason why this is a good thing is that for automated testing, you'd like the page to be in the same state every time a new test visits it.)

There are however ways to emulate cookies for testing with WebDriver.

Example

Consider the page http://www.cnn.com for example. Every time you run a new test against that page, a bar at the bottom of the page will pop up asking you to accept their cookie policy:

enter image description here

The page stores the fact that you once clicked the "I agree" button in a cookie itself. You can use the Chrome content settings to identify which cookies a certain web page sets. Details on that can be found here: https://superuser.com/questions/244062/how-do-i-view-add-or-edit-cookies-in-google-chrome

For this particular page, the name of the responsible cookie is "CNNtosAgreed" and the value it is set to once you click the "I agree" button is "true".

In your WebDriver script you can use the manage cookies functionality to set specific cookies, so that the page you're testing thinks you have been visiting it before.

In the Java client bindings this would look like that:

Java

driver.get("http://edition.cnn.com/");
driver.manage().addCookie(new Cookie("CNNtosAgreed", "true", ".cnn.com", "/", null));
driver.get("http://edition.cnn.com/");

Notice how the first time Chrome navigates to the page, the confirmation dialog pops up, but not the second time.

Here are some examples of what the cookie API exactly looks like in Python: How to save and load cookies using python selenium webdriver

Community
  • 1
  • 1
ralph.mayr
  • 1,320
  • 8
  • 12