Selenium with Firefor or Chrome profile

给你一囗甜甜゛ 提交于 2021-01-07 05:46:12

问题


I am trying to load a profile to selenium so that I dont have to keep log in to the website that selenium is about to visit. I am running it with python on my Mac.

In the firefox version, I use the below code

def create_selenium_FF():
    profile = webdriver.FirefoxProfile('/Users/Victor/Library/Application Support/Firefox/Profiles/z3ay0enb.default')
    driver = webdriver.Firefox(profile)
    return driver

It can successfully start firefox, but it doesn't have the log in info of the website that it visits, however I check in the automated firefox browser using about:profiles, it does recognise the profile that I feed it.

in the chrome version, I use the below code, notice I make a local copy of the profile already.

def create_selenium_chrome():
    DRIVER = 'chromedriver'
    options = webdriver.ChromeOptions()
    options.add_argument("--user-data-dir=/Users/Victor/Library/Application Support/Google/Chrome2")
    options.add_argument("--profile-directory=Default")
    driver = webdriver.Chrome(DRIVER, options=options)
    return driver   

it can also start chrome, and looks like it has my profile, but it raises an error

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir

How can I get it working please?


回答1:


I just solved the problem !

So here is my code :

from selenium import webdriver
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\\%username%\\AppData\\Local\\Google\\Chrome\\User Data 2")
driver = webdriver.Chrome(executable_path=path,options=options)
driver.get("https://www.google.com") 

The thing is, you'll get this error if there already is chrome opened on your computer !

So, i just copy / paste the folder User Data and rename the pasted one into User Data 2 so my chrome works with user data and selenium with user data 2 I guess.

I know that you've been waiting for a long time and I don't know if u still need this but here you got !!



来源:https://stackoverflow.com/questions/60048743/selenium-with-firefor-or-chrome-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!