【问题标题】:Selenium for Python attach to current session but doesn't load Chrome profileSelenium for Python 附加到当前会话,但不加载 Chrome 配置文件
【发布时间】:2021-11-26 12:10:25
【问题描述】:

我有一个 Python 脚本,它使用 Selenium 自动浏览网络。

我正在升级我的脚本以使其在 Chrome 已经打开的情况下也能正常工作,将驱动程序附加到当前会话。

我正在使用的部分代码是:

def attach_to_session(executor_url, session_id):
    original_execute = WebDriver.execute
    def new_command_execute(self, command, params=None):
        if command == "newSession":
            # Mock the response
            return {'success': 0, 'value': None, 'sessionId': session_id}
        else:
            return original_execute(self, command, params)
    # Patch the function before creating the driver object
    WebDriver.execute = new_command_execute
    driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
    driver.session_id = session_id
    # Replace the patched function with original function
    WebDriver.execute = original_execute
    return driver

service = Service('C:\\Users\\nicoc\\PycharmProjects\\WriteAI\\chromedriver.exe')

driver = webdriver.Chrome(service=service)

executor_url = driver.command_executor._url
session_id = driver.session_id
driver = attach_to_session(executor_url, session_id)

它工作的部分原因是,它正确打开了一个新的浏览器窗口并根据我的代码开始导航,同时打开了一个已经存在的 Chrome 会话,但是 它不会使用已经打开的会话正在使用的 Chrome 配置文件.

此外,更好的解决方案是在已打开的 Chrome 窗口上使用新标签打开驱动程序,而不是在启动驱动程序时打开新的 Chrome 窗口。

我仍然可以管理驱动程序打开一个新的 Chrome 窗口,只要它使用它在活动 Chrome 会话中使用的特定配置文件。

我已经尝试添加选项参数

driver = webdriver.Chrome(service=service, options=chrome_options)
chrome_options = Options()
chrome_options.add_argument\
    (r"--user-data-dir=C:\\Users\\nicoc\\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument(r'--profile-directory=Default')

但我收到一条错误消息,提示该配置文件已在使用中

在 chrome 实例打开时使用 driver.get("https://www.youtube.com/") 编辑会返回以下错误:

selenium.common.exceptions.InvalidArgumentException:消息:无效 参数:用户数据目录已被使用,请指定一个 --user-data-dir 参数的唯一值,或者不使用 --user-data-dir

如果我删除选项

chrome_options.add_argument(r"--user-data-dir=C:\\Users\\nicoc\\AppData\\Local\\Google\\Chrome\\User Data")

它会打开一个新的 Chrome 窗口,但同样没有加载配置文件

【问题讨论】:

  • but I got an error saying the profile is already in use - 关闭已经打开的Chrome浏览器,然后运行程序
  • @pmadhu 您还没有阅读我猜想的问题...关键是将驱动程序附加到与该用户的现有会话中。正如我所提到的,如果我关闭浏览器,一切正常;我想在浏览器已经打开的情况下启动我的脚本。
  • 我确实阅读了这个问题,但专注于profile is already in use 部分。我无法理解。您想在自动化过程中打开新标签还是在同一标签中打开新网址?
  • @pmadhu 最好打开一个新标签,但使用现有标签也可以。我只想将驱动程序附加到已经打开的现有 Chrome 浏览器。 PS:在我的问题中明确说明:“更好的解决方案是在已打开的 Chrome 窗口上使用新标签打开驱动程序”
  • 我曾尝试提及在浏览器中打开选项卡的方法。但我不确定这是否会对你有所帮助。

标签: python selenium google-chrome


【解决方案1】:

要打开一个新标签,您可以使用JavaScript

driver.get("https://www.youtube.com/")

# This opens a new tab and loads another URL
driver.execute_script("window.open('https://www.linkedin.com/');")
driver.get("https://www.youtube.com/")

# This opens a new URL in the same tab
driver.get("https://www.linkedin.com/")

要在现有选项卡中打开浏览器选项卡,您可以在 python 中使用webbrowser。但我不认为我们可以通过它实现自动化。

import webbrowser

webbrowser.open_new_tab("https://www.linkedin.com/")

您可能想参考的链接:Open web in new tab Selenium + Pythonhttps://docs.python.org/3/library/webbrowser.html

【讨论】:

  • 此解决方案不会附加到现有的 Chrome 实例。这只是打开驱动程序并打开一个新选项卡。在 Chome 实例已经打开的情况下,代码会返回一个错误(我已经测试过)。我很高兴也很感激你能帮助我,但请先试着理解我的问题。
  • 那是什么错误?
  • 用户已在使用中。用错误更新了我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 2013-01-06
  • 2016-03-23
相关资源
最近更新 更多