Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

How to use Chrome Profile in Selenium Webdriver Python 3

So whenever I try to use my Chrome settings (the settings I use in the default browser) by adding

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:Users... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)

it shows me the error code

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated UXXXXXXXX escape

in my bash. I don't know what that means and I'd be happy for any kind of help I can get. Thanks in advance!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:

  • To use the default Chrome Profile:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = webdriver.ChromeOptions()
    options.add_argument("user-data-dir=C:\Users\AtechM_03\AppData\Local\Google\Chrome\User Data\Default")
    driver = webdriver.Chrome(executable_path=r'C:pathochromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it. So as per the best practices create a new chrome profile for your @Test and store/save/configure within the profile the required data.

  • To use the customized Chrome Profile:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\Users\AtechM_03\AppData\Local\Google\Chrome\User Data\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:pathochromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • Here you will find a detailed discussion on How to open a Chrome Profile through Python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...