python - 消息 : Error: Polling for changes failed: N

我正在尝试在 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 文件中,并且拥有所有需要的权限。

我在谷歌上看到这可能是因为 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.”

关于python - 消息 : Error: Polling for changes failed: NetworkError when attempting to fetch resource while downloading file through Selenium and FirefoxProfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56364233/

相关文章:

git - 一个 git 子模块卡在错误的提交 ('submodule update' 不工作)

angular - 如何在构造函数中选择性地注入(inject) MAT_DIALOG_DATA

google-chrome - 本地主机 Web 应用程序访问远程 API 时未设置 Cookie

azure-active-directory - 企业应用程序中基于组角色的授权

reactjs - 我们如何在使用 react 最终形式时在组件中设置状态

python - 是否可以在 Snakemake 中有一个可选的输出文件?

sql-server - SQL Server Management Studio v18 的命令行

google-chrome-extension - Chrome 扩展程序 - 不在请求中发送 co

python-3.x - 如何在 Python 中创建具有每个变量值之间差距的堆积条形图

python - 无法编码响应 : datetime. datetime(2019, 5, 29,