Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
Why can't I get text with .text method when text exists?

I'm practicing web scraping using Selenium, and I hope to get the color of this dress. When I inspect the website, I can see the text content under the 'screen-reader-text' class but when I try to fetch it, I always get an empty value. What's going on? Is it because the Zara website blocks me to scrape it? My code is the following,

driver_path = 'D:/Python/Selenium/chromedriver'
option1 =  webdriver.ChromeOptions()
option1.add_experimental_option('detach',True)

driver = webdriver.Chrome(chrome_options=option1,executable_path=driver_path)
driver.get(url)

color = driver.find_element_by_xpath('//*[@id="main"]/article/div[1]/div[2]/div[1]/div[3]/ul/li[1]/button/span/span/span').text

Since I wish to get all the possible colors, I also tried the following code, which doesn't work as well:(

colors = driver.find_elements_by_xpath('//*[@id="main"]/article/div[1]/div[2]/div[1]/div[3]/ul')
col = []
for i,color in enumerate(colors):
    prefix = '//*[@id="main"]/article/div[1]/div[2]/div[1]/div[3]/ul'
    try: 
        col.append(color.find_elements_by_xpath(prefix+f'/li[{i}]'+'/button/span/span/span').text)
    except:
        pass
col

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's a SeleniumBase solution that gets the answer: (SeleniumBase is a Selenium Python framework.)

from seleniumbase import BaseCase

class MyTestClass(BaseCase):
    def test_example(self):
        self.open("https://www.zara.com/us/en/jacquard-mini-dress-p01198636.html?v1=140654644")
        elements = self.find_elements(".product-detail-color-selector__color-area span")
        print("\nColors:")
        for element in elements:
            print(element.get_property("textContent"))

Here's the output of that:

Colors:
Ecru / Brown
Printed

The important takeaway is that you need to use element.get_property("textContent") to get the text because the element itself is not visible, even though the text appears in the HTML. The solution also uses a cleaner selector to find the elements.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda