如何在普通浏览器中使用Selenium
我想知道能不能把selenium连接到我平时用的浏览器,而不是用驱动程序。我平时用的是Chrome浏览器,还装了很多插件,比如广告拦截器和Flash拦截器等等。我想用这种特定的配置来加载一个网站。我该怎么做呢?
补充一下 - 我不想像这个问题那样,只连接到一个已经打开的浏览器:
我不在乎是不是用驱动程序来启动进程。我只想要完整的浏览器配置,比如cookies、插件、字体等等。
谢谢!
1 个回答
11
首先,你需要下载ChromeDriver
,然后要么把它的路径放到PATH
环境变量里,要么在executable_path
这个参数里直接写上路径:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/executeable/chrome/driver')
为了加载扩展程序,你需要设置ChromeOptions
:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options=options)
你还可以保存你使用的Chrome用户配置文件,并将其加载到ChromeDriver
中:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=/path/to/my/profile')
driver = webdriver.Chrome(chrome_options=options)
另外,你可以参考: