简体   繁体   English

如何使用 Selenium 和 Python 提取小计价格

[英]How to extract the Subtotal Price using Selenium and Python

I am using Xpath and having trouble pulling out information I need.我正在使用 Xpath 并且无法提取我需要的信息。

It is basically a Price - Subtotal.它基本上是一个价格 - 小计。 The HTML Image is attached, and the xpath code I am using is below.附上 HTML 图像,我使用的 xpath 代码如下。 What am I missing?我错过了什么? At the end I am looking to get the price value into a string.最后,我希望将价格值转换为字符串。

w = driver.find_element_by_xpath('//*[@id="order-summary"]/section/section[3]/div/table/tbody/tr[2]/td/span')
print(w.text())

在此处输入图像描述

To print the Price - Subtotal text ie $27.99 you can use either of the following Locator Strategies :要打印价格 - 小计文本,即27.99 美元,您可以使用以下任一定位器策略

  • Using css_selector and get_attribute("innerHTML") :使用css_selectorget_attribute("innerHTML")

     print(driver.find_element(By.CSS_SELECTOR, "td.total-line__price > span.order-summary__emphasis").get_attribute("innerHTML"))
  • Using xpath and text attribute:使用xpath文本属性:

     print(driver.find_element(By.XPATH, "//td[@class='total-line__price']/span[contains(@class, 'order-summary__emphasis')]").text)

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies :理想情况下,您需要为visibility_of_element_located()引入WebDriverWait ,并且您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR and text attribute:使用CSS_SELECTORtext属性:

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "std.total-line__price > span.order-summary__emphasis"))).text)
  • Using XPATH and get_attribute("innerHTML") :使用XPATHget_attribute("innerHTML")

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[@class='total-line__price']/span[contains(@class, 'order-summary__emphasis')]"))).get_attribute("innerHTML"))
  • Note : You have to add the following imports:注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium_webdriver.support 按预期导入 EC 条件

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python您可以在如何使用 Selenium - Python 检索 WebElement 的文本中找到相关讨论


References参考

Link to useful documentation:链接到有用的文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM