在Selenium python中使用按类名查找元素来获取属性值

问题描述 投票:0回答:1

我正在尝试使用类名“favorites-video favorites-table-spacing”从视频列表中的属性“data-rbd-drag-handle-draggable-id”中获取视频ID,以便我可以将它们附加到列表中包含所有视频 ID。这是我正在使用的 HTML 示例。

<div class="favorites-video favorites-table-spacing" data-rbd-draggable-context-id="2" data-rbd-draggable-id="6568ff097b469e24dce6eae8key0" tabindex="0" role="button" aria-describedby="rbd-hidden-text-2-hidden-text-10" data-rbd-drag-handle-draggable-id="6568ff097b469e24dce6eae8key0" data-rbd-drag-handle-context-id="2" draggable="false">


<div class="favorites-video favorites-table-spacing" data-rbd-draggable-context-id="2" data-rbd-draggable-id="6568ff097b469e24dce6eae8key0" tabindex="0" role="button" aria-describedby="rbd-hidden-text-2-hidden-text-10" data-rbd-drag-handle-draggable-id="6568ff097b469e24dce6eae8key0" data-rbd-drag-handle-context-id="2" draggable="false">


<div class="favorites-video favorites-table-spacing" data-rbd-draggable-context-id="2" data-rbd-draggable-id="6552711226e176c233b46379key2" tabindex="0" role="button" aria-describedby="rbd-hidden-text-2-hidden-text-10" data-rbd-drag-handle-draggable-id="6552711226e176c233b46379key2" data-rbd-drag-handle-context-id="2" draggable="false">

我使用检查元素确保类名是唯一的。

我尝试将 By.CLASS_NAME 与以下代码一起使用,但没有返回任何内容,仅返回 id_videolist 的 [] 。为什么硒不能抓住该元素?

id_videolist = []

favvideos = driver.find_elements(By.CLASS_NAME, 'favorites-video favorites-table-spacing') 
for video in favvideos:     
    vidId = video.get_attribute('data-rbd-drag-handle-draggable-id') 
    vidId = vidId[:-4]
    id_videolist.append(vidId)
html selenium-webdriver selenium-chromedriver
1个回答
0
投票

使用这个 表达式:

'//*[@data-rbd-drag-handle-draggable-id]/@data-rbd-drag-handle-draggable-id' 
© www.soinside.com 2019 - 2024. All rights reserved.