gpt4 book ai didi

python - (Python, Selenium) 是否只有属性满足条件才能获取文本列表?

转载 作者:太空宇宙 更新时间:2023-11-04 04:29:56 24 4
gpt4 key购买 nike

不确定我在标题中表达了我的观点。

先上源码

<tbody>
<tr>
<td>
<p>
<label>
Want to
Get these texts
only if img exist
</label>
<img src="Some td tags don't contain img" alt="alt"></img>
</p>
</td>
</tr>

<tr>
<td>
<p>
<label>
Want to
Get these texts
only if img exist
</label>
</p>
</td>
</tr>
...
...
...
</tbody>

正如上面所说,有很多(大约 1,000)组标签。在这些内部,有时存在具有特定 alt 值的标签(它们都是相同的),而有时不存在(就像第二部分一样。)

所以我要做的是仅当标签文本具有标签作为其同级时才获取标签文本。到目前为止,没有任何好处。

我们将不胜感激任何人的帮助!

(附:我刚刚进入 python 世界,所以多一点描述将不胜感激。提前致谢!)

最佳答案

您可以使用下面获取所需文本值的列表

labels = [label.text for label in driver.find_elements_by_xpath('//label[following-sibling::img[@alt="alt"]]')]

关于python - (Python, Selenium) 是否只有属性满足条件才能获取文本列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52859856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com