我正在網站上抓取價格資料:https ://fbx.freightos.com/ 。
這是下面的代碼:
from selenium import webdriver from selenium.webdriver.edge.service import Service from selenium.webdriver.common.by import By
s = 服務(e_driver_path) 驅動程式 = webdriver.Edge(service=s)
元素 = driver.find_elements(By.XPATH,
/html/body/div[2]/div[1]/div[3]/div/div[1]/section/div[2]/div/div[2]/div/div[1]/div/div/div[1]/span)content = "".join([element.text for element in elements])
列印(內容)
問題是結果。這是一個空串列。按照計劃,它應該是“當前 FBX”,結果看起來像“$3,540”。請幫忙。
提前謝謝。
uj5u.com熱心網友回復:
嘗試等待所需資料
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
fbx = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[.='Current FBX']/following-sibling::span[normalize-space()]"))).text
uj5u.com熱心網友回復:
使用webdriverwait()并等待位于和跟隨 css 選擇器的元素的可見性
driver.get("https://fbx.freightos.com/")
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".row.bottom-xs.between-xs div:nth-of-type(1)>div"))).text)
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".row.bottom-xs.between-xs div:nth-of-type(1)>span"))).text)
您需要匯入以下庫。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
uj5u.com熱心網友回復:
此 xpath 將直接針對價格 web 元素。正如@KunduK 提到的,在等待一段時間后嘗試獲取這個
//div[contains(text(),'Current FBX')]/parent::div/span
uj5u.com熱心網友回復:
//span[@class='styled__Value-sc-1puuh0x-8 hIASbB']
您也可以像這樣使用 xpath。簡單是最好的
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516084.html
