samedi 5 septembre 2020

python selenium gives me an empty string for text

I have an HTML page that contains 40 of the following div

<div class='movie-featured'>
    <div class="item analytics">
        <div class="movie-details">
            <div class="movie-rating-wrapper">
                <span class="movie-rating-summary">
                    <span>some text</span>
                </span>
            </div>
        </div>
    </div>
</div>

and I'm trying to get the text from this span <span>some text</span> rom inside each one of the 40 divs via: find_element_by_css_selector('span.moview-rating-summary').find_element_by_tag_name('span').text

OUTPUT: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '6/10', '', '', '', '', '', '', '', '', '7.5/10', '', '', '', '', '']

as you can see, I only get text from few spans and not all of them.

I also tried: find_element_by_tag_name('span').get_attribute('textContent') and find_element_by_tag_name('span').get_attribute('innerHTML').

but still the same result

any ideas how to fix that??

my FULL CODE:

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
browser = webdriver.Chrome()
delay = 10 
browser.get("www.example.com")


browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
time.sleep(2)
images =[]

myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'item-responsive')))


body = browser.find_element_by_class_name('movie-featured') # body of images container

imageItems = body.find_elements_by_css_selector('div.item.analytics')  #list of divs that hold movies images


for item in imageItems:
    
    rate = item.find_element_by_css_selector('span.moview-rating-summary').text

    images.append(rate)
    
print(images)
browser.close()



Aucun commentaire:

Enregistrer un commentaire