有一份报告,我想从中提取一个值
该值由用 java 脚本编写的组件生成
当我检查下图部分的元素时,这是突出显示的特定部分代码
下面是它和 XPath 的代码
<span title="£19,649,981.19" class="right-Align">£19,650.0k</span>
XPATH: //*[@id="HEADER_HEADER_3_ADVANCE_HEADER_0LOAN_OUTSTANDING"]/span
Full XPATH: /html/body/div[3]/div[3]/div[1]/headers/div/header[1]/div/div/div[1]/div[1]/h1/span/span
这样我就可以访问我通过打印页面标题确认的URL
但是当尝试获取值时它返回空列表
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager
# Setup Edge WebDriver
edge_service = EdgeService(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=edge_service)
try:
WebDriverWait(driver, 30).until(EC.url_contains("dashboard"))
desired_url = "Example.com"
driver.get(desired_url)
time.sleep(10)
print("Title of the Webpage is =", driver.title)
span_elements = driver.find_elements(By.XPATH, '/html/body/div[3]/div[3]/div[1]/headers/div/header[1]/div/div/div[1]/div[1]/h1/span/span')
print(span_elements)
span_elements1 = driver.find_elements(By.XPATH, '//*[@id="HEADER_HEADER_3_ADVANCE_HEADER_0LOAN_OUTSTANDING"]/span')
print(span_elements1)
finally:
# Close the browser
driver.quit()
Output:
Title of the Webpage is =Overview Data
[]
[]
但我需要获得价值 £19,649,981.19
注意:此网页从通过 Javascript 编写的组件获取值
您的 xpath 查询可能是错误的。 发布 html,以便我们可以尝试查找错误