简体   繁体   English

消息:错误:轮询更改失败:在通过 Selenium 和 FirefoxProfile 下载文件时尝试获取资源时出现网络错误

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

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:我正在尝试在 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

I use geckodriver verssion 0.22 and firefow version 65.0.我使用 geckodriver 0.22 版和 firefow 65.0 版。 Also am on UBUNTU 18 (only ssh) geckodriver is in the /usr/bin file and have all the needed right.同样在 UBUNTU 18(仅 ssh)上,geckodriver 位于 /usr/bin 文件中,并且拥有所有需要的权限。

I have read on google that this might be because of the COPS.我在谷歌上读到这可能是因为 COPS。 But I really get what the COPS are or how to do to fix them (if that is the real problem).但我真的明白 COPS 是什么或如何解决它们(如果这是真正的问题)。

here my code:这是我的代码:

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

Do you guys have any idea ?你们有什么想法吗?

This error message...这个错误信息...

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

...implies that there was a NetworkError while attempting to fetch resource. ...暗示在尝试获取资源时出现NetworkError

Here the main issue probably is related to Cross-Origin Resource Sharing (CORS)这里的主要问题可能与跨域资源共享(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.跨源资源共享 (CORS) 是一种机制,它使用额外的 HTTP 标头告诉浏览器让在一个源(域)运行的 Web 应用程序有权访问来自不同源的服务器的选定资源。 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.当 Web 应用程序请求与自己的来源具有不同来源(域、协议和端口)的资源时,它会发出跨源 HTTP 请求。

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://domain-a.com提供的 Web 应用程序的前端 JavaScript 代码使用 XMLHttpRequest 向http://api.domain-b.com/data.json发出请求.

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.出于安全原因,浏览器会限制从脚本内发起的跨源 HTTP 请求。 For example, XMLHttpRequest and the Fetch API follow the same-origin policy.例如,XMLHttpRequest 和 Fetch API 遵循同源策略。 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.这意味着使用这些 API 的 Web 应用程序只能从加载应用程序的同一来源请求 HTTP 资源,除非来自其他来源的响应包含正确的 CORS 标头。

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.但是这个新标准意味着服务器必须处理新的请求和响应头。

Solution解决方案

You need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:您需要引入WebDriverWait以使所需元素可点击,您可以使用以下解决方案:

  • 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:快照:

download_file_firefox

I got the same error.我得到了同样的错误。 After updating the geckodriver vresion to geckodriver 0.24.0 ( 2019-01-28) worked fine for me.将 geckodriver vresion 更新为 geckodriver 0.24.0 ( 2019-01-28) 后,对我来说效果很好。 Try this试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 GeckoDriver 和 Firefox 到 Selenium 的“无法获取区域”(新错误(“NO_RESULT”、“resource://gre/modules/Region.jsm”、376))错误 - “Failed to fetch region” (new Error(“NO_RESULT”, “resource://gre/modules/Region.jsm”, 376)) error using GeckoDriver and Firefox through Selenium 如何通过Selenium和Python加载现有的FirefoxProfile - How to load a existing FirefoxProfile through Selenium and Python 失败-在Selenium无头Chrome中下载时出现下载错误 - Failed - Download Error when downloading in Selenium headless Chrome 失败:下载由 jupyter notebook 生成的 Excel 文件时出现网络错误 - failed: Network error while downloading Excel file generated by jupyter notebook 在python中通过Selenium Webdriver下载文件 - Downloading file through Selenium Webdriver in python 尝试为Python 3安装scikit-learn时出现“无法获取索引库URL”错误消息 - “Cannot fetch index base URL” error message when attempting to install scikit-learn for Python 3 FirefoxProfile 与 Selenium 的私有模式 - FirefoxProfile with private mode for Selenium 使用 Selenium 下载文件时出现“失败 - 下载错误” Python - "Failed - Download error" while download a file using Selenium Python selenium.common.exceptions.InvalidArgumentException:消息:尝试通过selenium通过url上传图像时找不到文件 - selenium.common.exceptions.InvalidArgumentException: Message: File not found while trying to upload image by url through selenium 尝试POST时出现“ getaddr信息失败”错误 - “getaddr info failed” Error When Attempting POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM