Home > Software design >  Can't find an element even though its brother element is found in Selenium Python?
Can't find an element even though its brother element is found in Selenium Python?

Time:11-03

I'm trying to get the content from a tag, but it raised NoSuchElement even though getting it from an another tag with the same level is successful.

This is the link to website: https://soundcloud.com/pastlivesonotherplanets/sets/spell-jars-from-mars

This is the html code that I access to:

<div class="fullHero__tracksSummary">
      <div class="playlistTrackCount sc-font">
            <div class="genericTrackCount sc-font large m-active" title="16 tracks">
                  <div class="genericTrackCount__title">16</div>
                  <div class="genericTrackCount__subtitle"> Tracks </div>
                  <div class="genericTrackCount__duration sc-type-small sc-text-body sc-type-light sc-text-secondary">56:07</div>
            </div>
      </div>
</div>

I'm trying to get the playlist's duration with this code:

try:
   tmp=driver.find_element_by_xpath("//div[@class='fullHero__tracksSummary']") 
   duration=tmp.find_element_by_class_name("genericTrackCount__duration sc-type-small sc-text-body 
   sc-type-light sc-text-secondary").get_attribute('textContent')
   print(duration)
except:
   print("None")

It raised error NoSuchElement even though the other two tags was successful.

What is the problem and how can I fix it?

Thank your for your time.

CodePudding user response:

Without looking at the page, you probably need to wait for the element to load.

You can use either time.sleep(5) 5 being the number of seconds to wait or WebDriverWait(driver, 20) with an expected condition

so your code would look like

import expected_conditions as EC
    
  WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, '"genericTrackCount__duration sc-type-small sc-text-body 
       sc-type-light sc-text-secondary"))).text

Also maybe the get_attribute('textContent') is failing, you can just use .text

CodePudding user response:

You can also do that using xapth as follows:

`WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@]/div[1]'))).text

#import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

CodePudding user response:

I think you can try directly using xpath //div[contains(@class, 'duration')] OR //div[contains(@class, 'playlistTrackCount')]/descendant::div[contains(@class, 'duration')]

  • Related