【问题标题】:selenium chrome attach default profileselenium chrome 附加默认配置文件
【发布时间】:2013-03-07 13:44:43
【问题描述】:

我正在使用 python ana selenium 来自动化某些过程,但无法将 selenium 附加到默认的 chrome 配置文件 我试过了,

capability = webdriver.DesiredCapabilities.CHROME
self.driver = webdriver.Remote('http://127.0.0.1:9515/wd/hib',capability)

当然,我开始了,chromedriver先,也试过了,

import time
from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('./chromedriver')
service.start()
capabilities = {'chrome.binary': '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
driver.quit()

这会导致 selenium.common.exceptions.WebDriverException: Message: u'Could not find Chrome binary at:

也试过了,

self.driver = webdriver.Chrome("./chromedriver")

这可行,但不是默认配置文件,并且还想知道如何使用它打开新窗口或新标签?

谢谢。

【问题讨论】:

标签: python selenium pyqt


【解决方案1】:

不要直接从网站上复制/粘贴内容!自己看看那个文件夹,里面有什么吗?!我的猜测是没有。这就是为什么当你离开那一点时,它可以正常工作,因为它正在寻找应该存在的 Chrome!

无论如何,更重要的是您错误地使用它!

如果您想为 Selenium 提供不同的配置文件以用于 Chrome,那么您需要使用 options 类:

https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/chrome/options.py

您想要add_argument 函数。

为什么?

这是因为要让 Chrome 使用另一个配置文件,您需要使用特定的命令行(特别是 --user-data-dir)启动 Chrome:

http://www.chromium.org/user-experience/user-data-directory

add_argument 函数公开了添加命令行开关的能力。

因此,如果您使用 add_argument 函数,Selenium 将简单地将您提供的任何内容传递给 Chrome,作为其命令行开关的一部分。

【讨论】:

    【解决方案2】:

    要找出您的 chrome 配置文件所在的位置,请启动 chrome 并键入

    chrome://version
    

    在地址栏中。在“配置文件路径:”下,您将看到您当前使用的配置文件的位置。例如:

    ~:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
    

    【讨论】:

      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2019-12-01
      • 2015-09-12
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多