Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!