为什么Selenium不能加载firefox个人资料?

时间:2015-05-06 03:53:08

标签: python firefox selenium-webdriver

以下代码:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import os.path
import os
FIREFOX_PATH = os.path.join("C:", 
                            "FirefoxPortable", "FirefoxPortable.exe")
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(FIREFOX_PATH)
driver = webdriver.Firefox(firefox_binary=binary)

# go to the google home page
driver.get("http://www.google.com")

因投诉无法加载个人资料而失败:

$ python test_selenium.py
Traceback (most recent call last):
  File "test_selenium.py", line 17, in <module>
    driver = webdriver.Firefox(firefox_profile=profile,firefox_binary=binary)
  File "/usr/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
    self._wait_until_connectable()
  File "/usr/lib/python2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 105, in _wait_until_connectable
    raise WebDriverException("Can't load the profile. Profile "
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.

有什么想法发生了什么?

我承认我不关心配置文件(并且只有最模糊的感觉) - 我只想加载一个基本的浏览器,这样我就可以自动化一些网页抓取。

非常感谢! / YGA

1 个答案:

答案 0 :(得分:0)

根据this问题,指定路径如下工作。这会在FirefoxPortable中搜索D:/文件夹。

FIREFOX_PATH = os.path.join("D:/", "FirefoxPortable", "FirefoxPortable.exe")

此外,下面的代码工作正常。我删除了import os行,因为它是多余的。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import os.path

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

FIREFOX_PATH = os.path.join("D:/", "FirefoxPortable", "FirefoxPortable.exe")
binary = FirefoxBinary(FIREFOX_PATH)
driver = webdriver.Firefox(firefox_binary=binary)

# go to the google home page
driver.get("http://www.google.com")
driver.quit()