【问题标题】:Selenium with Firefox: FirefoxProfile stops working when used a second timeSelenium 与 Firefox:FirefoxProfile 在第二次使用时停止工作
【发布时间】:2021-09-07 11:45:01
【问题描述】:

我正在尝试使用 selenium 和 firefox 来创建我想重复使用的浏览器的配置文件。我首先使用我希望它具有的扩展加载它,然后使用该配置文件作为参数调用一个函数以用于抓取。

profile = webdriver.FirefoxProfile()
profile.add_extension(extension='extension/1/path')
profile.add_extension(extension='extension/2/path')


def sele_scrape(profile,url):

  options = Options()
  options.headless = True

  driver = webdriver.Firefox(firefox_profile=profile,options=options,executable_path='path/to/geckodriver')

  driver.get(url)
  source=driver.page_source
  driver.quit()


sele_scrape(profile,'url1')
sele_scrape(profile,'url2')

网址的顺序或我使用的网址无关紧要。第二个总是导致这个错误:

File "(the python file for this project)", line 37, in <module>
    sele_scrape(profile,'url2')
  File "(the python file for this project)"
    driver = webdriver.Firefox(firefox_profile=profile,options=options,executable_path='path/to/geckodriver')

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 166, in __init__
    capabilities.update(options.to_capabilities())
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/firefox/options.py", line 180, in to_capabilities
    opts["profile"] = self._profile.encoded
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 173, in encoded
    self.update_preferences()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 102, in update_preferences
    self._write_user_prefs(self.default_preferences)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 226, in _write_user_prefs
    with open(self.userPrefs, "w") as f:

FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/8j/lrckmwqs5bs9b4srrjbj6r5c0000gn/T/tmpiluv90lz/user.js'

这一行

FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/8j/lrckmwqs5bs9b4srrjbj6r5c0000gn/T/tmpiluv90lz/user.js'

对我来说特别奇怪,因为我不知道它试图指的是什么或为什么。

任何帮助,甚至只是我可以查看的方向,将不胜感激!

【问题讨论】:

  • 如果你想重复使用它,那么可能只创建一次driver,不要quit()它。
  • 可能当您 quit() 时,它会删除配置文件 - 您必须重新创建配置文件。
  • 如果我使用 close() 而不是 quit(),代码对我有用

标签: python selenium selenium-webdriver web-scraping selenium-firefoxdriver


【解决方案1】:

可能当您 quit() 时,它会删除个人资料 - 您必须重新创建个人资料。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# --- functions ---

def sele_scrape(url):

    profile = webdriver.FirefoxProfile()
    #profile.add_extension(extension='extension/1/path')
    #profile.add_extension(extension='extension/2/path')
    
    options = Options()
    #options.headless = True
    
    driver = webdriver.Firefox(firefox_profile=profile, options=options)#,executable_path='path/to/geckodriver')
    
    driver.get(url)
    source = driver.page_source
    driver.quit()

# --- main ---

sele_scrape('https://stackoverflow.com')
sele_scrape('https://stackoverflow.com')

如果我使用 close() 而不是 quit(),代码可以正常工作

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# --- functions ---

def sele_scrape(url, profile, options):
   
    driver = webdriver.Firefox(firefox_profile=profile, options=options)#,executable_path='path/to/geckodriver')
    
    driver.get(url)
    source = driver.page_source
    driver.close()

# --- main ---

profile = webdriver.FirefoxProfile()
#profile.add_extension(extension='extension/1/path')
#profile.add_extension(extension='extension/2/path')

options = Options()
#options.headless = True

sele_scrape('https://stackoverflow.com', profile, options)
sele_scrape('https://stackoverflow.com', profile, options)

坦率地说,如果应该重复使用配置文件,那么我将只创建一次 driver,然后我会跳过 close()

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# --- functions ---

def sele_scrape(url, driver):
   
    driver.get(url)
    source = driver.page_source

# --- main ---

profile = webdriver.FirefoxProfile()
#profile.add_extension(extension='extension/1/path')
#profile.add_extension(extension='extension/2/path')

options = Options()
#options.headless = True

driver = webdriver.Firefox(firefox_profile=profile, options=options)#,executable_path='path/to/geckodriver')

sele_scrape('https://stackoverflow.com', driver)
sele_scrape('https://stackoverflow.com', driver)

driver.close()

【讨论】:

  • 谢谢!这绝对有效,但我对您建议的实施有疑问。首先, .close 不是简单地关闭打开的选项卡,而 quit 完全关闭浏览器吗?但是,如果我加载扩展,他们不会打开其他在 .close 之后保持打开的选项卡吗?无论如何,这就是 chrome 所做的
  • 第二,我很好奇你不关闭的想法。澄清一下,我的程序不只是一个接一个地抓取一堆网址。相反,它运行了很长时间,然后偶尔调用硒刮板。在那种情况下,如果我不使用 .quit 或 .close,我会不会在我做其他事情时一直打开一个 firefox 窗口,这有点浪费?您认为不关闭 Firefox 会提高我的执行速度吗?因为我正在尝试尽可能优化它
  • 如果您不退出,则浏览器一直在内存中,但重新启动浏览器不需要几秒钟。当您在 selenium 中运行许多 url 时,保持打开状态会很有用。如果您偶尔使用它,那么您可能看不到差异,但它仍然可以运行几秒钟(或几分钟)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多