我正在使用 Xpath 并且無法提取我需要的資訊。
它基本上是一個價格 - 小計。附上 HTML 影像,下面是我使用的 xpath 代碼。我錯過了什么?最后,我希望將價格值放入一個字串中。
w = driver.find_element_by_xpath('//*[@id="order-summary"]/section/section[3]/div/table/tbody/tr[2]/td/span')
print(w.text())

uj5u.com熱心網友回復:
要列印價格 - 小計文本,即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)
理想情況下,你需要引起WebDriverWait的visibility_of_element_located(),你可以使用以下的定位策略:
使用
CSS_SELECTOR和文本屬性: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 import expected_conditions as EC
您可以在How toret the text of a WebElement using Selenium - Python 中找到相關討論
參考
鏈接到有用的檔案:
get_attribute()方法Gets the given attribute or property of the element.text屬性回傳The text of the element.- 使用 Selenium 的 text 和 innerHTML 之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369601.html
