Ole*_*xiy 4 python selenium-chromedriver
我的代码:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
opts = Options()
opts.add_argument("user-data-dir=/path/to/profiles_dir/user_id")
browser = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=opts)
Run Code Online (Sandbox Code Playgroud)
当我为 id = 1 的用户启动 Chromium 时,它开始正常并创建一个配置文件目录 /path/to/profiles_dir/1。然后我访问某个任意站点并关闭浏览器。当我第二次执行上面的代码时,它抛出异常。
selenium.common.exceptions.WebDriverException:消息:未知错误:无法解析内部 JSON 模板:行:1,列:1,意外标记。(驱动信息:chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.4.0-112-generic x86_64)
Chromium 64.0.3282.119 基于 Ubuntu 构建,在 Ubuntu 16.04 上运行
ChromeDriver 2.35
硒 3.8.1
我用谷歌搜索了很多,但找不到这个问题的解决方案。为什么我不能使用现有的用户配置文件目录加载浏览器?我究竟做错了什么?
小智 9
chromedriver 中似乎有一个错误。我缩小了范围,两个文件似乎是罪魁祸首:{user-data-dir}/Local State和{user-data-dir}/{profile-directory}/Preferences. 如果您不指定profile-directory,它将是“默认”。
Chrome/Chromium 似乎无法读取这些文件,即使您使用browser.quit().
您需要删除这些文件才能使用相同的配置文件再次启动 chromedriver。
我在我的finally块中使用了以下代码,以删除文件:
if browser is not None:
browser.quit()
time.sleep(1)
delete_paths = ['../selenium/chrome_profile/Local State',
'../selenium/chrome_profile/Default/Preferences']
for delete_path in delete_paths:
if os.path.exists(delete_path):
os.remove(delete_path)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3384 次 |
| 最近记录: |