Python Selenium previous::查找元素错误

标签 python selenium

我愿意

  1. 查找链接中包含“2021”的所有标题
  2. 获取所有链接、标题和日期

我使用:

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/

相关文章:

selenium - CSS 和 XPath 选择器有什么区别?就跨浏览器测试的性能而言,哪个更好?

android - 无法在模拟器或真实设备上使用 Appium 打开 Android 浏览器

java - 如何点击页面刷新时 id 不断变化的日期选择器

python - 将文本拆分为逻辑 block

python - Pandas:从 JSON 文件检索嵌套数据

python - 基维,导入错误 : cannot import name App

python - 从给定索引列表的 HDFStore 中选择行

c# - 如何重复失败的测试,以便它仅在 x 次重复后失败?

java - Selenium 网络驱动程序 : How to call a class from selenium Junit program

python - Numpy 在读取 CSV 时将科学计数法转换为 nan