Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource while downloading file through Selenium and FirefoxProfile

Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource while downloading file through Selenium and FirefoxProfile

我正在尝试在 python3 上使用 selenium 和 Firefox 从 url 下载文件,但这在 geckodriver 日志文件中给我一个错误:

 (firefox:13723): Gtk-WARNING **: 11:12:39.178: Theme parsing error:       <data>:1:77: Expected ')' in color definition
 1546945960048  Marionette  INFO    Listening on port 40601
 1546945960132  Marionette  WARN    TLS certificate errors will be ignored for this session
     console.error: BroadcastService: 
      receivedBroadcastMessage: handler for
      remote-settings/monitor_changes
       threw error:
            Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource..
            Stack:
                remoteSettingsFunction/remoteSettings.pollChanges@resource://services-settings/remote-settings.js:188:13

我使用 geckodriver 版本 0.22 和 firefow 版本 65.0。我也在 UBUNTU 18(仅限 ssh) geckodriver 在 /usr/bin 文件中并且拥有所有需要的权限。

我在 google 上看到这可能是因为 COPS。但我真的了解 COPS 是什么或如何解决它们(如果这是真正的问题)。

这是我的代码:

from os import getcwd
from pyvirtualdisplay import Display
from selenium import webdriver

# start the virtual display
display = Display(visible=0, size=(800, 600))
display.start()

# configure firefox profile to automatically save csv files in the current directory
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")

driver = webdriver.Firefox(firefox_profile=fp)
page = "https://www.thinkbroadband.com/download"
driver.get(page)
driver.find_element_by_xpath("//*[@id='main-col']/div/div/div[8]/p[2]/a[1]").click()

你们有什么想法吗?

这个错误信息...

Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource..

...表示在尝试获取资源时出现 NetworkError

这里的主要问题可能与Cross-Origin Resource Sharing (CORS)

有关

Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin. A web application makes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, and port) than its own origin.

An example of a cross-origin request: The frontend JavaScript code for a web application served from http://domain-a.com uses XMLHttpRequest to make a request for http://api.domain-b.com/data.json.

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request HTTP resources from the same origin the application was loaded from, unless the response from the other origin includes the right CORS headers.

Modern browsers handle the client-side components of cross-origin sharing, including headers and policy enforcement. But this new standard means servers have to handle new request and response headers.

解决方案

您需要引入 WebDriverWait 以使所需的 元素可点击,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from os import getcwd
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # configure firefox profile to automatically save csv files in the current directory
    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList", 2)
    fp.set_preference("browser.download.manager.showWhenStarting", False)
    fp.set_preference("browser.download.dir", getcwd())
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    driver = webdriver.Firefox(firefox_profile=fp, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("https://www.thinkbroadband.com/download")
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='specific-download-headline' and contains(., 'Extra Small File (5MB)')]//following::p[1]/a"))).click()
    
  • 快照:

  • 参考:How to resolve “TypeError: NetworkError when attempting to fetch resource.”

我遇到了同样的错误。将 geckodriver vresion 更新为 geckodriver 0.24.0 ( 2019-01-28 ) 后对我来说工作正常。试试这个

xxxxx:~$ geckodriver --version
geckodriver 0.24.0 ( 2019-01-28)