【发布时间】:2021-12-15 19:54:19
【问题描述】:
我正在抓取一个页面,我正在寻找以下列表中的所有元素:
description = driver.find_elements(By.XPATH, "//div[contains(@class,'list') and h1/text()='Values']/ul[@class='bullet-list']")
但给定页面上的数量未知,主要在 3-10 之间。每个列表中还有未知数量的项目(每个列表中的项目数不匹配)。如果我这样搜索列表项:
description = driver.find_elements(By.XPATH, "//div[contains(@class,'list') and h1/text()='Values']/ul[@class='bullet-list']/li")
我得到了所有列表项,但现在我不知道它们属于哪个列表。
我想做的是这样的
description = driver.find_elements(By.XPATH, "//div[contains(@class,'list') and h1/text()='Values']/ul[@class='bullet-list']")
for x in range(len(description)):
values = driver.find_elements(By.XPATH, f"{description[x].xpath}/li")
for y in values:
b_list.append(y.text)
a_list.append(b_list)
所以你可以明白为什么我需要知道这些项目来自哪个列表,以便它们可以在同一个 [] 中。
我可以使用绝对 xpath 来解决这个问题,但如果我能找到使用相对 xpath 的方法会更好。
【问题讨论】:
标签: python html selenium xpath