Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting selenium to launch safari with default profile in python

I'm trying to launch Safari with Selenium in python with all my sessions logged in (e.g. gmail) so I don't have to login manually.

The easy solution would be to launch safari with the default user profile, but I can't find documentation on how to do this.

from selenium import webdriver
driver = webdriver.Safari()
url = 'https://www.gmail.com/'
driver.get(url)

Just for reference, the code below is the code for Chrome. What is the safari equivalent?

options.add_argument("user-data-dir=/Users/alexiseggermont/Library/Application Support/Google/Chrome/Default/") #Path to your chrome profile
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)
like image 485
Alexis Eggermont Avatar asked Jan 28 '18 19:01

Alexis Eggermont


2 Answers

There is setDataDir() function in SafariOptions on safari driver v2.43.1 according to document as following however I couldn't find any document about how to use it and there is no information about last versions:

* @param dataDir A File object pointing to the Safari installation's data directory.
* If {@code null}, the default installation location for the current platform will be used.


  public void setDataDir(File dataDir) {
    this.dataDir = Optional.fromNullable(dataDir);
  }
like image 45
slckayhn Avatar answered Sep 29 '22 10:09

slckayhn


From what I have read, Safari does not have user profiles. If you mean to start Safari from a different Mac user I would suggest doing it with bash:

sudo -u username python open.py

where username is the username of the user and open.py is your python file.

like image 175
Aidan H Avatar answered Sep 29 '22 09:09

Aidan H