Selenium Python:不打印元素文本

时间:2017-10-26 19:15:59

标签: python html python-3.x selenium

您好我正在浏览网页以查找包含特定文字的元素,然后根据项目是否已售罄,点击或不点击它们。但是当我在找到我想要的文本后尝试打印元素文本时,它不会打印出文本。它要么不打印,要么只是换行:

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 :{")

以下是我正在查看的页面部分

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

2 个答案:

答案 0 :(得分:1)

.text 提供WebElement的innerText。你没有看到文字,因为你的小册子里没有。

如果你看看你的HTML:

enter image description here

您可以注意到感兴趣的信息位于&#34; innerText&#34;属性。

因此,要打印所有这些所有这些信息的信息:

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  

答案 1 :(得分:0)

尝试选择“product-item__meta__inner”并将其存储在与“first_product check”不同的变量中:

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