python – 消息:错误:轮询更改失败:尝试通过Selenium和FirefoxProfile下载文件时获取资源时出现NetworkError
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 消息:错误:轮询更改失败:尝试通过Selenium和FirefoxProfile下载文件时获取资源时出现NetworkError,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4111字,纯文字阅读大概需要6分钟。
内容图文
![python – 消息:错误:轮询更改失败:尝试通过Selenium和FirefoxProfile下载文件时获取资源时出现NetworkError](/upload/InfoBanner/zyjiaocheng/707/3478cdecb0f84f938e3a43b16984a45b.jpg)
我试图在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 verssion 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 07001 uses XMLHttpRequest to make a request for 07002.
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 – 消息:错误:轮询更改失败:尝试通过Selenium和FirefoxProfile下载文件时获取资源时出现NetworkError全部内容,希望文章能够帮你解决python – 消息:错误:轮询更改失败:尝试通过Selenium和FirefoxProfile下载文件时获取资源时出现NetworkError所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。