How to use an existing google chrome profile with selenium chrome webdriver in python?

ぃ、小莉子 提交于 2020-01-05 07:34:51

问题


I need to load my full existing google chrome profile with all chrome extensions where i logged in google and site account. I'm struggling with this code, syntax error somewhere

chrome_options = Options()
chrome_options.add_argument('user-data-dir= C:\Users\DMMaxim\AppData\Local\Google\Chrome\User Data')
capabilities = DesiredCapabilities.CHROME.copy()
chromedriver = r"C:\Users\DMMaxim\Desktop\chromedriver_win32\chromedriver.exe" 
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options = chrome_options, desired_capabilities=capabilities)

i get this error

C:\Users\DMMaxim\Desktop>python ExportbacktestTradingview.py
  File "ExportbacktestTradingview.py", line 21
    chrome_options.add_argument('--user-data-dir=C:\Users\DMMaxim\AppData\Local\Google\Chrome\User Data')
                               ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 18-19: truncated \UXXXXXXXX escape

Here is my full code

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import DesiredCapabilities
from subprocess import Popen
from time import gmtime, strftime
from os import system
import subprocess
import pyperclip
import datetime
import time
import os
import sys

timer=0
chrome_options = Options()
chrome_options.add_argument('user-data-dir= C:\Users\DMMaxim\AppData\Local\Google\Chrome\User Data')
capabilities = DesiredCapabilities.CHROME.copy()
chromedriver = r"C:\Users\DMMaxim\Desktop\chromedriver_win32\chromedriver.exe" 
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options = chrome_options, desired_capabilities=capabilities)

driver.get("https://www.tradingview.com/chart/gK6Rq0UH/")
driver.implicitly_wait(90)
driver.find_element_by_xpath("""//*[@id="footer-chart-panel"]/div[2]/span[4]""").click()
driver.implicitly_wait(90)
while True:
    driver.implicitly_wait(90)
    driver.find_element_by_xpath("""//*[@id="bottom-area"]/div[2]/div[1]/div[2]/ul/li[4]""").click()
    driver.implicitly_wait(90)
    timer=timer+1
    time.sleep(1)
    if timer > 250:
                process1 = subprocess.Popen(['C:/Users\DMMaxim/AppData/Local/Programs/Python/Python36-32/python.exe', 'C:/Users/DMMaxim/Desktop/Export backtest Tradingview.py'], shell=True)
                driver.quit()                                             
    driver.switch_to_alert().accept()
    contents = pyperclip.paste()
    filepath = r"C:\Users\DMMaxim\Desktop\DATA.txt"
    with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f
        f.write(contents)

回答1:


chromedriver = r"C:\Users\DMMaxim\Desktop\chromedriver_win32\chromedriver.exe"



来源:https://stackoverflow.com/questions/49460190/how-to-use-an-existing-google-chrome-profile-with-selenium-chrome-webdriver-in-p

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