簡體   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