繁体   English   中英

消息:错误:轮询更改失败:在通过 Selenium 和 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 文件中,并且拥有所有需要的权限。

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

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

跨源资源共享 (CORS) 是一种机制,它使用额外的 HTTP 标头告诉浏览器让在一个源(域)运行的 Web 应用程序有权访问来自不同源的服务器的选定资源。 当 Web 应用程序请求与自己的来源具有不同来源(域、协议和端口)的资源时,它会发出跨源 HTTP 请求。

跨域请求的示例:从http://domain-a.com提供的 Web 应用程序的前端 JavaScript 代码使用 XMLHttpRequest 向http://api.domain-b.com/data.json发出请求.

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

现代浏览器处理跨域共享的客户端组件,包括标头和策略执行。 但是这个新标准意味着服务器必须处理新的请求和响应头。

解决方案

您需要引入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()
  • 快照:

download_file_firefox

我得到了同样的错误。 将 geckodriver vresion 更新为 geckodriver 0.24.0 ( 2019-01-28) 后,对我来说效果很好。 试试这个

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM