I want to get all em and the following a tags, but these are seperated:
<em style="color: #FF2500;">ITEM:</em> <a href="LINK"> LINK </a><br />
<em style="color: #FF2500;">ITEM2:</em> <a href="LINK2"> LINK2 </a><br />
<em style="color: #FF2500;">ITEM3:</em> <a href="LINK3">LINK3 </a><br />
I need to save the ITEM and the correspinding LINK, because they must be together, but I only managed to find the text of the links:
elems = driver.find_elements_by_xpath("//em/following-sibling::a[@href]")
printing this gives me only the contents of the link:
elems = driver.find_elements_by_xpath("//em/following-sibling::a[@href]")
for link in elems:
print (link.text) # LINK, LINK2, LINK3
I could of course find all em and links by themself, but I wouldnt know if they fit together. So I need to find:
All <em>
where an <a>
with a certain text follows. This way I should know for sure that they are together.
答案 0 :(得分:1)
使用em
a
之前的text
//a[text()=' LINK2 '] | //a[text()=' LINK2 ']/preceding-sibling::em[1]
只获取两个元素连接的文本
concat(//a[text()=' LINK2 ']/preceding-sibling::em[1], //a[text()=' LINK2 '])
结果:
"ITEM2: LINK2 "