Cant get text while webscraping a site

Viewed 42

I'm trying to get the part where it says Avvisami on this website: https://www.nike.com/it/launch/t/womens-air-jordan-3-sp-a-ma-maniere to appear as a string on my code. Every time I try anything it doesn't work. This is the part of the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions() 

options.add_argument(r'--user-data-dir=C:\Users\mainuser\AppData\Local\Google\Chrome\User Data')
options.add_argument('--profile-directory=Profile 1')

driver = webdriver.Chrome(options = options)
driver.get('https://www.nike.com/it/launch/t/womens-air-jordan-3-sp-a-ma-maniere')

instock = (driver.find_elements_by_class_name('ncss-btn-primary-dark  btn-lg'))

print(instock)

and in that, this is the part I think I need to change:

instock = (driver.find_elements_by_class_name('ncss-btn-primary-dark  btn-lg'))

print(in stock)

I've been trying to fix it for an hour or so but I just can't wrap my head around how.

2 Answers
instock = driver.find_element_by_css_selector(".ncss-btn-primary-dark.btn-lg").text
print(instock)

Multiple class names should be used with css selector and to grab the text just use .text and then place it in your variable.

You are trying to get a text from a list of elements. Iterate it and use .text:

elems = driver.find_elements_by_css_selector(".ncss-btn-primary-dark.btn-lg")
for el in elems:
    print(el.text)

All of these elements are buttons.

Related