Selenium Python: doesn't print the elements text

Hello so I am going through a webpage to find elements with a certain text and then either click or not click on them based on if the items are sold out or not. However when I try to print out the element text after I find the ones I want, it does not print out the text. It either does not print or I just get newlines:

browser = webdriver.Chrome()
browser.get("https://hoshiikins.com/") #navigates to hoshiikins.com
items = browser.find_elements_by_class_name("product-item__link") #finds all of the page products and puts them in a list
wanted_item = "husky" #keyword in product herf that lables the product we want
first_product_sold_check = items[0].find_element_by_class_name("product-item__meta__inner") 
first_product_sold_check = first_product_sold_check.find_element_by_class_name("product-item__sold-out")
first_product_sold_check = first_product_sold_check.getText()
print(first_product_sold_check + "This is gonnee")
second_product_sold_check = items[0].find_element_by_xpath("//*[contains(text(), 'Sold out')]")
print(str(second_product_sold_check.text) + "Its sold out :{")

And here is the part of the page that I am looking at

<div class="product-item grid__item medium-up--one-half">
    <a class="product-item__link  product-item__image--margins" href="/products/byo-husky-tail-preorder">
        <img class="product-item__image" src="//cdn.shopify.com/s/files/1/1409/8528/products/image_ba9bfdf5-7dde-424e-8ee7-765b372d70bf_grande.jpg?v=1506191375" alt="BYO Husky Tail *PREORDER*">

        <span class="product-item__meta">
      <span class="product-item__meta__inner">

          <p class="product-item__vendor">Hoshiikins</p>

        <p class="product-item__title">BYO Husky Tail *PREORDER*</p>
        <p class="product-item__price-wrapper">

                <span class="visually-hidden">Regular price</span> $36

        </p>

        <p class="product-item__sold-out">Sold out</p>

        </span>
        </span>

    </a>
</div>

Upvotes: 1

Views: 1695

Answers (2)

Davide Patti
Davide Patti

Reputation: 3471

The .text provide the innerText of a WebElement. You don't see the text because there aren't in your webelements.

If you take a look to your html:

enter image description here

You can notice that the interested info are in the "innerText" attribute.

So, to print for example all these infos of all the webelements:

driver.get("https://hoshiikins.com/") #navigates to hoshiikins.com                                                   
spanList= driver.find_elements_by_xpath("//span[@class='product-item__meta']")                                       
i=1                                                                                                                  
for span in spanList:                                                                                                
    vendor=span.find_element_by_xpath(".//p[@class='product-item__vendor']").get_attribute("innerText")              
    print("vendor: " + vendor)                                                                                       
    title= span.find_element_by_xpath(".//p[@class='product-item__title']").get_attribute("innerText")               
    print("title: " + title)                                                                                         
    price_wrapper=span.find_element_by_xpath(".//p[@class='product-item__price-wrapper']").get_attribute("innerText")
    print("price_wrapper: " + price_wrapper)                                                                         
    sold_out=span.find_element_by_xpath(".//p[@class='product-item__sold-out']").get_attribute("innerText")          
    print("sold_out: " + sold_out)                                                                                   
    print("-------------------------- "+str(i)+" --------------------------")                                        
    i=i+1  

Upvotes: 1

Recalc
Recalc

Reputation: 23

Try selecting "product-item__meta__inner" and storing it in a different variable than "first_product check" :

first_product_name = items[0].find_element_by_class_name("product-item__title") 

Upvotes: 0

Related Questions