<div>
<div class = âclass1â >
<div class = âclass2â >
<div class = âclass3â >
<div style = âclear: both; â >
</div>
<div>
<div class = âclass1â >
<div class = âclass2â >
<div class = âclass3â >
<div style = âclear: both; â >
</div>
<div>
<div class = âclass1â >
<div class = âclass2â >
<div class = âclass3â >
<div style = âclear: both; â >
</div>
每个部分都有不同的信息。我想在类中搜索一个特定的词,如果这个词存在,那么我打印信息。在这之后,我有了问题。在我想得到本节第三节课的信息之后。例如,如果我在第一节中的类1有“this word”,那么我想在这节中获得类3的信息。
cs1 = driver.find_elements_by_class_name("class1")
for i in cs1:
information = i.text
if "this word" in information:
print(information)
infclass3 = i.find_element_by_xpath('//following-sibling::div[@class = "class3"]')
print(infclass3.text)
问题是:我用“this word”获得了1级信息,但这一节中关于3级的信息我没有。每次它都会打印第一节中的class3。例如,如果“this word”在第二节和第三节中,我会得到这样的结果:
information of class1 - Section 2
information of class3 - Section 1
information of class1 - Section 3
information of class3 - Section 1
那么第1行和第3行中的信息是正确的。但在第2行和第4行中不是,1。因为是重复2。因为在第一节中不是“这个词”
谢谢你的帮助。
我希望你有一个愉快的一天:)