Selenium Python: 查找下一个元素和/或查找变化的元素

huangapple go评论74阅读模式
英文:

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&amp;stock_id=402513825&amp;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:

&lt;div tabindex=&quot;0&quot; class=&quot;item-img&quot; style=&quot;background-image:url(&amp;quot;//images.neopets.com/items/snowfood_chiapopblueberry.gif&amp;quot;);&quot; alt=&quot;Mmmm, a frozen blueberry Chia Pop.  Just what your Neopet needs to cool down.&quot; title=&quot;Mmmm, a frozen blueberry Chia Pop.  Just what your Neopet needs to cool down.&quot; border=&quot;1&quot; onclick=&quot;confirmPurchase(this)&quot; onkeyup=&quot;clickElement(event)&quot; data-name=&quot;Blueberry Chia Pop&quot; data-price=&quot;560&quot; data-link=&quot;haggle.phtml?obj_info_id=8590&amp;amp;stock_id=402513825&amp;amp;g=3&quot;&gt;&lt;/div&gt;

Code I have tried:

time.sleep(10)
driver.get(&quot;https://www.neopets.com/objects.phtml?type=shop&amp;obj_type=37&quot;)
time.sleep(5)
driver.find_element(By.XPATH, &quot;//*[contains(text(), &#39;Blueberry Chia Pop&#39;)]&quot;)
print(&quot;Item In Stock&quot;)
item = driver.find_element(By.XPATH, &quot;//div[@data-name=&#39;Blueberry Chia Pop&#39;]&quot;)
item_price = item.find_element(By.XPATH, &quot;following-sibling::td&quot;).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&amp;stock_id=402513825&amp;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:

&lt;div tabindex=&quot;0&quot; class=&quot;item-img&quot; style=&quot;background-image:url(&amp;quot;//images.neopets.com/items/snowfood_chiapopblueberry.gif&amp;quot;);&quot; alt=&quot;Mmmm, a frozen blueberry Chia Pop.  Just what your Neopet needs to cool down.&quot; title=&quot;Mmmm, a frozen blueberry Chia Pop.  Just what your Neopet needs to cool down.&quot; border=&quot;1&quot; onclick=&quot;confirmPurchase(this)&quot; onkeyup=&quot;clickElement(event)&quot; data-name=&quot;Blueberry Chia Pop&quot; data-price=&quot;560&quot; data-link=&quot;haggle.phtml?obj_info_id=8590&amp;amp;stock_id=402513825&amp;amp;g=3&quot;&gt;&lt;/div&gt;	

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, &quot;div[data-name=&#39;Blueberry Chia Pop&#39;]&quot;).get_attribute(&quot;data-price&quot;))
    
  • Using xpath:

    print(driver.find_element(By.XPATH, &quot;//div[@data-name=&#39;Blueberry Chia Pop&#39;]&quot;).get_attribute(&quot;data-price&quot;))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    

huangapple
  • 本文由 发表于 2023年6月8日 05:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定