此错误消息…
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 ECconfigure 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:UtilityBrowserDriversgeckodriver.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()



