5

I am currently building a reporting tool with Selenium on Adwords. (And no, I cannot use Google's API because it doesn't provide the functionality I want).

I am trying to avoid logging in and logging out because I understand frequent logins and logouts are not as convenient as cookie-based authentication.

I have the following code: save.py

try:
  driver = webdriver.Chrome()
  driver.get('https://adwords.google.com')
  time.sleep(90)
  # Manually login to adwords page and wait
  pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

finally: driver.close()

And this: load.py

try:
  driver = webdriver.Chrome()
  cookies = pickle.load(open("cookies.pkl", "rb"))
  for cookie in cookies:
    driver.add_cookie(cookie)

driver.get('https://adwords.google.com/') time.sleep(60)

finally: driver.close()

When I first run load.py, I am actually able to see the spinner that shows up when one logs into Adwords. Shortly after however, I get logged out!

I don't know what is causing Google's authentication system to log me out. What do you think is the cause of this?

JAINAM
  • 1,835
  • 2
  • 18
  • 39
Hot dog
  • 61
  • 2
  • 1
    I wish you luck but your question is way too specific to current version of Google AdWords to be of a general interest on this forum now, and in the future. I suggest to search for some forum specifically for Google AdWords, or as a last resort find a relevant blog posting and comment on it. You get bonus point for using Python! :-) – Peter M. - stands for Monica Oct 30 '17 at 17:19

1 Answers1

1

I think this is because you are closing the webdriver window and disposing the profile. Google will track your session and for safety reasons will not allow to log in to account from different browser with the same session. Try to save your browser profile and load it for each test. Profile, session and cookies will be same and they shouldn't have problem with it then.

I am sorry i can't help you with saving and loading profile because i use C# and JS for testing, but you can try this:

https://stackoverflow.com/questions/31062789/how-to-load-default-profile-in-chrome-using-python-selenium-webdriver

It will use your default chrome profile, but should be just few changes away from pretty great solution.

nEkis
  • 131
  • 2