[英]How to extract the price of the book from website using Selenium Python
[英]How to extract the Subtotal Price using Selenium and Python
要打印價格 - 小計文本,即27.99 美元,您可以使用以下任一定位器策略:
使用css_selector
和get_attribute("innerHTML")
:
print(driver.find_element(By.CSS_SELECTOR, "td.total-line__price > span.order-summary__emphasis").get_attribute("innerHTML"))
使用xpath
和文本屬性:
print(driver.find_element(By.XPATH, "//td[@class='total-line__price']/span[contains(@class, 'order-summary__emphasis')]").text)
理想情況下,您需要為visibility_of_element_located()
引入WebDriverWait ,並且您可以使用以下任一Locator Strategies :
使用CSS_SELECTOR
和text屬性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "std.total-line__price > span.order-summary__emphasis"))).text)
使用XPATH
和get_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"))
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium_webdriver.support 按預期導入 EC 條件
鏈接到有用的文檔:
get_attribute()
方法Gets the given attribute or property of the element.
text
屬性返回The text of the element.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.