我愿意
- 查找链接中包含“2021”的所有标题
- 获取所有链接、标题和日期
我使用:
all_links=[link.get_attribute('href') for link in driver.find_elements_by_xpath("//span[contains(text(),'2021'/preceding::a)]")]
titles = [title.text for title in driver.find_elements_by_xpath("//span[contains(text(),'2021') ]")]
dates = [date.text for date in driver.find_elements_by_xpath("//span[contains(text(),'2021')]/following::td/span")]
for link, title, date in zip(links, titles, dates):
...
错误:语法错误:无法对“文档”执行“评估”:评估表达式时类型转换失败。 ( session 信息:chrome=88.0.4324.182)
链接:http://www.szse.cn/disclosure/bond/notice/index.html
非常感谢!
最佳答案
对于链接
,请尝试使用此 xpath:
//span[@class='pull-left ellipsis title-text' and contains(text(), '2021')]
从上面的 xpath 中,您可以使用 //parent::a
实现 href
,使用 date
实现strong>//ancestor::td//following-sibling::td
。
wait = WebDriverWait(driver, 20)
links = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='pull-left ellipsis title-text' and contains(text(), '2021')]")))
titles = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='pull-left ellipsis title-text' and contains(text(), '2021')]//parent::a")))
dates = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='pull-left ellipsis title-text' and contains(text(), '2021')]//ancestor::td//following-sibling::td")))
for link, title, date in zip(links, titles, dates):
print(link.text)
print(title.get_attribute("href"))
print(date.text)
关于Python Selenium previous::查找元素错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66362178/