如何在 python 中使用现有的 google chrome 配置文件和 selenium chrome webdriver?
Posted
技术标签:
【中文标题】如何在 python 中使用现有的 google chrome 配置文件和 selenium chrome webdriver?【英文标题】:How to use an existing google chrome profile with selenium chrome webdriver in python? 【发布时间】:2018-09-02 18:02:57 【问题描述】:我需要加载我现有的完整 google chrome 配置文件,其中包含我登录 google 和站点帐户的所有 chrome 扩展。 我正在为这段代码苦苦挣扎,某处出现语法错误
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)
我得到这个错误
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
这是我的完整代码
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)
【问题讨论】:
chromedriver = r"C:\Users\DMMaxim\Desktop\chromedriver_win32\chromedriver.exe" 【参考方案1】:chromedriver = r"C:\Users\DMMaxim\Desktop\chromedriver_win32\chromedriver.exe"
【讨论】:
以上是关于如何在 python 中使用现有的 google chrome 配置文件和 selenium chrome webdriver?的主要内容,如果未能解决你的问题,请参考以下文章
如何在现有的旧版Android项目中使用最新的API添加Google Map?
如何将 Spark-BigQuery_connector 用于现有的 spark 环境(不使用 google dataproc)
如何在现有的 Cognito 用户池中获取生日? (放大 iOS)