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

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

问题描述

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

我的默认配置文件在这里:~/Library/Caches/Google/Chrome/Default

这是我目前的设置方式:(不工作)

from selenium import webdriver选项 = 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 版本

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

有关详细信息,请参阅此链接.

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

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

参考:

http://chromedriver.chromium.org/capabilities

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.

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")

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

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.

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.

解决方案

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

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

Refer to this link for more information.

How to create a custom profile:

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.

Reference:

http://chromedriver.chromium.org/capabilities

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

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