硒:指向默认的Chrome会话 [英] Selenium: Point towards default Chrome session

查看:84
本文介绍了硒:指向默认的Chrome会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我意识到这不是好的"做法-我有一个用例,需要将Selenium驱动程序指向(挂钩)我的默认Chrome会话/配置文件.

Though I realize it's NOT "good" practice - I have a use case where I need to point (hook up) the Selenium driver to my default Chrome session/profile.

我的默认个人资料在这里: ~/Library/Caches/Google/Chrome/Default

My default profile is here: ~/Library/Caches/Google/Chrome/Default

这是我当前的设置方式:(不起作用)

Here is how I'm seting it up currently: (not working)

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Caches/Google/Chrome")
options.add_argument("--profile-directory=Default")
browser = webdriver.Chrome(options=options, executable_path=r"./chromedriver")
browser.get("http://google.com")

我正在使用Chrome版本74.0.3729.169和chromedriver版本 ChromeDriver 74.0.3729.6 (兼容版本).

I'm using Chrome version 74.0.3729.169 and chromedriver version ChromeDriver 74.0.3729.6 (which is the compatible version).

Chrome打开后,我在Chrome的设置中看不到任何Cookie,因此很明显它没有指向我的默认会话.另外,我看到已经创建了Selenium目录(这似乎意味着它未能连接到~/Library/Caches/Google/Chrome/Default上的会话.

When Chrome opens I don't see any cookies in Chrome's settings so it's clear it's NOT being pointed to my default session. Also, I see that a Selenium directory has been created (which appears to mean that it has failed to connect to the session at ~/Library/Caches/Google/Chrome/Default.

如何将硒连接到默认的Chrome会话?这与正常打开Chrome浏览器时看到的会话相同.

How do I hook up selenium to my default Chrome session? This is the same session as one sees when normally opening up Chrome.

我已经查看了其他

I've looked at this other question, but the answer there fails to address how to point Selenium towards default session. Also - it's an outdated question - Chrome and Chromedriver have progressed a lot since then. Also, the question there assumes that the poster is able to connect to default session - I am not able to do that which suggests that the Chromedriver/Chrome have changed since then. Also that question is for Windows - I'm on a Mac where things work differently.

推荐答案

请确保您使用"Chrome://version"指向正确的文件夹.

Make sure you are pointing to the right folder using "Chrome://version".

我正在使用Windows,但在Mac情况下也应该类似.

I am using the windows but it should be similar in you mac case too.

有关更多信息,请参见此链接.

Refer to this link for more information.

如何创建自定义配置文件:

您可以通过仅在用户数据目录开关设置为某个新目录的情况下运行Chrome(在命令行中或通过ChromeDriver)来创建自己的自定义配置文件. 如果该路径不存在,Chrome会在指定位置创建一个新的配置文件.然后,您可以根据需要修改配置文件设置,ChromeDriver将来可以使用该配置文件 .在浏览器中打开chrome://version,以查看Chrome使用的配置文件.

You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.

参考:

http://chromedriver.chromium.org/capabilities

这篇关于硒:指向默认的Chrome会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆