skip to Main Content

I am trying to grab video IDs from the attribute "data-rbd-drag-handle-draggable-id" from a list of videos using class name "favorites-video favorites-table-spacing" so I can append them to a list containing all the video IDs. Here is an example of the HTML I am using.

<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">

I made sure using the inspect element that the class name is unique.

I tried using By.CLASS_NAME with the below code which didn’t return anything, only [] for id_videolist. How come selenium isn’t able to grab the element?

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)

2

Answers


  1. Use this expression:

    '//*[@data-rbd-drag-handle-draggable-id]/@data-rbd-drag-handle-draggable-id' 
    

    :

    favvideos = driver.find_elements(By.XPATH, '//*[@data-rbd-drag-handle-draggable-id]/@data-rbd-drag-handle-draggable-id')
    
    Login or Signup to reply.
  2. By.CLASS_NAME only works for a single class segment. Eg. "favorites-video" or "favorites-table-spacing", but not both together.

    Also expressed as a By.CSS_SELECTOR -> "div.favorites-video" or "div.favorites-table-spacing".

    For using them together, that would be 'div[class="favorites-video favorites-table-spacing"]' as a By.CSS_SELECTOR.

    Your updated line could be:

    favvideos = driver.find_elements(By.CLASS_NAME, "favorites-video")
    

    Or you can try:

    favvideos = driver.find_elements(By.CSS_SELECTOR, "div.favorites-video")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search