消息:错误:轮询更改失败:通过Selenium和FirefoxProfile下载文件时尝试获取资源时出现NetworkError [英] Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource while downloading file through Selenium and FirefoxProfile

查看:593
本文介绍了消息:错误:轮询更改失败:通过Selenium和FirefoxProfile下载文件时尝试获取资源时出现NetworkError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to download file from a url using selenium and Firefox on python3 but that give me an error in the geckodriver log file:

 (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 verssion 0.22和firefow版本65.0.同样在UBUNTU 18上(仅ssh) geckodriver位于/usr/bin文件中,具有所有需要的权限.

I use geckodriver verssion 0.22 and firefow version 65.0. Also am on UBUNTU 18 (only ssh) geckodriver is in the /usr/bin file and have all the needed right.

我在Google上已阅读到这可能是由于COPS所致.但是我确实了解了COPS的含义或解决方法(如果那是真正的问题).

I have read on google that this might be because of the COPS. But I really get what the COPS are or how to do to fix them (if that is the real problem).

这是我的代码:

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 .

...implies that there was a NetworkError while attempting to fetch resource.

主要问题可能与跨域资源共享有关(CORS)

跨源资源共享(CORS)是一种机制,该机制使用其他HTTP标头来告诉浏览器,使运行在一个来源(域)上的Web应用程序有权访问来自另一个来源的服务器中的选定资源.当Web应用程序请求其来源(域,协议和端口)与其来源不同的资源时,它将发出跨域HTTP请求.

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.

跨域请求的示例:从 http://domain- a.com 使用XMLHttpRequest对 http://api.domain- b.com/data.json .

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.

出于安全原因,浏览器限制了从脚本内部发起的跨域HTTP请求.例如,XMLHttpRequest和Fetch API遵循同源策略.这意味着使用这些API的Web应用程序只能从加载该应用程序的同一来源请求HTTP资源,除非来自其他来源的响应包括正确的CORS标头.

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 以使所需的元素可点击,并且可以使用以下解决方案:

Solution

You need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

  • 代码块:

  • Code Block:

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()

  • 快照:

  • Snapshot:

    这篇关于消息:错误:轮询更改失败:通过Selenium和FirefoxProfile下载文件时尝试获取资源时出现NetworkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆