英文:
Selenium Python: Find Next Element and/or Find Changing Element
问题
尝试查找商品的价格元素并返回并存储该属性。商品名称已清晰定义,但价格会变化。
元素 HTML:
<div tabindex="0" class="item-img" style="background-image:url('//images.neopets.com/items/snowfood_chiapopblueberry.gif');" alt="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." title="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." border="1" onclick="confirmPurchase(this)" onkeyup="clickElement(event)" data-name="Blueberry Chia Pop" data-price="560" data-link="haggle.phtml?obj_info_id=8590&stock_id=402513825&g=3"></div>
我尝试的代码:
time.sleep(10)
driver.get("https://www.neopets.com/objects.phtml?type=shop&obj_type=37")
time.sleep(5)
driver.find_element(By.XPATH, "//*[contains(text(), 'Blueberry Chia Pop')]")
print("Item In Stock")
item = driver.find_element(By.XPATH, "//div[@data-name='Blueberry Chia Pop']")
item_price = item.find_element(By.XPATH, "following-sibling::td").get_attribute("data-price")
请注意,我添加了 .get_attribute("data-price")
以获取价格属性的值。
英文:
Trying to find the price element of an item and return and store that attribute. The item name is clearly defined but the price varies.
Element HTML:
<div tabindex="0" class="item-img" style="background-image:url(&quot;//images.neopets.com/items/snowfood_chiapopblueberry.gif&quot;);" alt="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." title="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." border="1" onclick="confirmPurchase(this)" onkeyup="clickElement(event)" data-name="Blueberry Chia Pop" data-price="560" data-link="haggle.phtml?obj_info_id=8590&amp;stock_id=402513825&amp;g=3"></div>
Code I have tried:
time.sleep(10)
driver.get("https://www.neopets.com/objects.phtml?type=shop&obj_type=37")
time.sleep(5)
driver.find_element(By.XPATH, "//*[contains(text(), 'Blueberry Chia Pop')]")
print("Item In Stock")
item = driver.find_element(By.XPATH, "//div[@data-name='Blueberry Chia Pop']")
item_price = item.find_element(By.XPATH, "following-sibling::td").get_attribute()
答案1
得分: 0
给定以下HTML:
<div tabindex="0" class="item-img" style="background-image:url('//images.neopets.com/items/snowfood_chiapopblueberry.gif');" alt="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." title="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." border="1" onclick="confirmPurchase(this)" onkeyup="clickElement(event)" data-name="Blueberry Chia Pop" data-price="560" data-link="haggle.phtml?obj_info_id=8590&stock_id=402513825&g=3"></div>
假设 item name 为 "Blueberry Chia Pop", item price 为 "560",要打印价格,您可以使用以下任一 定位策略:
- 使用 css_selector:
print(driver.find_element(By.CSS_SELECTOR, "div[data-name='Blueberry Chia Pop']").get_attribute("data-price"))
- 使用 xpath:
print(driver.find_element(By.XPATH, "//div[@data-name='Blueberry Chia Pop']").get_attribute("data-price"))
- 注意:您需要添加以下导入语句:
from selenium.webdriver.common.by import By
英文:
Given the HTML:
<div tabindex="0" class="item-img" style="background-image:url(&quot;//images.neopets.com/items/snowfood_chiapopblueberry.gif&quot;);" alt="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." title="Mmmm, a frozen blueberry Chia Pop. Just what your Neopet needs to cool down." border="1" onclick="confirmPurchase(this)" onkeyup="clickElement(event)" data-name="Blueberry Chia Pop" data-price="560" data-link="haggle.phtml?obj_info_id=8590&amp;stock_id=402513825&amp;g=3"></div>
Assuming the item name being "Blueberry Chia Pop" and the item price being "560" to print the price you can use either of the following locator strategies:
-
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "div[data-name='Blueberry Chia Pop']").get_attribute("data-price"))
-
Using xpath:
print(driver.find_element(By.XPATH, "//div[@data-name='Blueberry Chia Pop']").get_attribute("data-price"))
-
Note : You have to add the following imports :
from selenium.webdriver.common.by import By
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论