我知道您已经接受了一个答案,但它实际上并没有解释问题是什么.主要问题是
single_element = wait.until(EC.visibility_of_element_located())
^ singular element
返回单个元素.它有一个复数版本,返回一组元素,
element_collection = wait.until(EC.visibility_of_all_elements_located())
^ plural elementS
使用此信息,我们可以更新您当前的代码,
driver = webdriver.Chrome(options=options)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
url = 'http://www.biblioteca.presidencia.gov.br/presidencia/ex-presidentes/jose-sarney/discursos/1985?b_start:int=0'
driver.get(url)
links = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "h2 a")))
for link in links:
print(link.text)
print(link.get_attribute("href"))
其输出
15 de março de 1985 - Pronunciamento do Presidente José Sarney, no exercício da Presidência da República, ao empossar o ministério
http://www.biblioteca.presidencia.gov.br/presidencia/ex-presidentes/jose-sarney/discursos/1985/01.pdf/view
17 de março de 1985 - Pronunciamento da Presidente José Sarney, no exercício da Presidência da república, por ocasião da abertura da primeira reunião do ministério da nova República
http://www.biblioteca.presidencia.gov.br/presidencia/ex-presidentes/jose-sarney/discursos/1985/02.pdf/view
...
另外,请不要使用定位器,例如
.find_elements("css selector", "h2 a")
其中"css selector"等是字符串.是的,它可以工作,但很容易出现拼写错误,并且直到您运行脚本并且它失败后,您的IDE才会知道拼写错误.相反,如下所示使用By.CSS_SELECTOR
.find_elements(By.CSS_SELECTOR, "h2 a")
您的IDE将帮助您自动完成它,如果存在拼写错误,IDE将在您运行它之前将其标记为错误,从而节省时间.