Python 硒蟒蛇:不';t打印元素文本
您好,我正在浏览一个网页,查找带有特定文本的元素,然后根据物品是否售完,单击或不单击它们。但是,当我在找到想要的元素文本后尝试打印出元素文本时,它不会打印出文本。它要么不打印,要么我只得到换行:Python 硒蟒蛇:不';t打印元素文本,python,html,python-3.x,selenium,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-it
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>
尝试选择“产品-项目\元\内部”并将其存储在与“首次产品检查”不同的变量中:
.text提供WebElement的内部文本。您看不到文本,因为您的webelements中没有 如果您查看您的html: 您可以注意到感兴趣的信息位于“innerText”属性中 例如,要打印所有Web元素的所有这些信息:
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
非常感谢。我没有意识到您必须使用它作为属性来获取,而不是它自己的函数!
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