硒:在Webelement中的特定文本之后获取所有元素
我们有以下HTML:
<div>
<img alt="Guest" >
Bobby gave:
<img>
<img>
<img>
<img>
and took
<img>
<img>
</div>
我想在第一个文本和第二文本之间获得所有图像元素。然后在第二文本之后分开所有IMG元素。
IMG元素的数量各不相同,因此以下硒代码无法正常工作:
message = driver.find_element(By.tag_name, 'div')
imgs_1 = message.find_elements(By.tag_name, 'img')[1:4]
imgs_2 = message.find_elements(By.tag_name, 'img')[5:]
XPATH或其他内容的任何建议?
We have the following Html:
<div>
<img alt="Guest" >
Bobby gave:
<img>
<img>
<img>
<img>
and took
<img>
<img>
</div>
I want to get all image elements between the first text and the second text. And then seperatly all the img elements after the second text.
The amount of img elements varies so the following selenium code wont work:
message = driver.find_element(By.tag_name, 'div')
imgs_1 = message.find_elements(By.tag_name, 'img')[1:4]
imgs_2 = message.find_elements(By.tag_name, 'img')[5:]
Any suggestions with xpath or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够缩短此代码或为您的需求进行自定义。解释您想要的事情很漫长。在这里是:
输出:
现在。我给了你
节点
列表。如果您的要求有进一步的复杂性,假设您的HTML中有三个文本/字符串值,则需要IMG或第二个文本值之间的其他类型元素,那么您应该能够从该python列表中获取这些元素使用一点逻辑。You should be able to shorten this code or customize it for your needs. It is lengthy to explain things you want. Here it is:
Output:
Now. I gave you
nodes
list. If you have further complexity in your requirements, let's say you have three text/string values in your HTML and you want IMG or some other type elements between second and third text values, then you should be able to get those elements from that Python list using a little bit of logic.如果您都知道两个文本值,则可以在
'Bobby给出:'
和'之间选择图像,然后
在第二个文本节点之后使用XPATH images进行',您可以使用
If you know both text values then you can select images between
'Bobby gave:'
and'and took'
with XPathImages after second text node you can select with