Python 消息:错误:轮询更改失败:NetworkError在通过Selenium和FirefoxProfile下载文件时尝试获取资源
我试图在python3上使用selenium和Firefox从url下载文件,但geckodriver日志文件中出现错误:Python 消息:错误:轮询更改失败:NetworkError在通过Selenium和FirefoxProfile下载文件时尝试获取资源,python,selenium,webdriverwait,firefox-profile,Python,Selenium,Webdriverwait,Firefox Profile,我试图在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
(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
你们知道吗?这个错误消息
Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource..
…表示尝试获取资源时出现网络错误
这里的主要问题可能与
跨源资源共享(CORS)是一种机制,它使用附加的HTTP头通知浏览器,让在一个源(域)上运行的web应用程序有权从另一个源的服务器访问选定的资源。当web应用程序请求的资源的来源(域、协议和端口)与其自己的来源不同时,会发出跨来源HTTP请求
跨源请求的一个示例:提供服务的web应用程序的前端JavaScript代码使用XMLHttpRequest请求
出于安全原因,浏览器限制从脚本中启动的跨源HTTP请求。例如,XMLHttpRequest和FetchAPI遵循相同的源策略。这意味着使用这些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()
- 快照:
- 参考:
- 我也犯了同样的错误。将geckodriver vresion更新为geckodriver 0.24.0(2019-01-28)后,对我来说效果很好。试试这个
xxxxx:~$ geckodriver --version
geckodriver 0.24.0 ( 2019-01-28)
我看不出等待一个元素被点击与CORS有什么关系。答案中提到CORS的部分毫无意义,也没有任何帮助。谢谢!这很有帮助。此外,我有错误的文件类型。这不是“文本/csv”,我也没有看到。我之前下载了一个插件来获取文件格式,我在“text/csv”之后添加了它。最后,这就是工作!再次感谢:)你打算用你在下面发表的评论来回答你自己的问题吗(错误的文件类型?)。
xxxxx:~$ geckodriver --version
geckodriver 0.24.0 ( 2019-01-28)