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