Python Selenium 通过 javascript 网站获取工具提示文本的值

Python Selenium get value of tooltip text with javascript website

我是 Selenium 的新手。阅读许多历史帖子并尝试检索工具提示文本但失败...

X 路径(“col_issued_shares”应该是正确的,因为它是直接复制的,如下图所示:

Screenshot 1

结果是语法错误:语法无效。我的预期产出是已发行股票 20,428,671,958(截至 2022 年 5 月 27 日)。但是当我查看源代码时,它会显示工具包文本。

Screenshot 2

Screenshot 3

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


s=Service(r'C:\Users\chromedriver.exe')
driver = webdriver.Chrome(service=s)
url='https://www.hkex.com.hk/Market-Data/Securities-Prices/Equities/Equities-Quote?sym=5&sc_lang=en'
driver.get(url)

element = driver.find_element(By.XPATH, "//*[@id="lhkexw-quoteequities"]/div[2]/div[3]/div[2]/div/div[2]/div[1]/div[1]/span[2]/span")

有人可以帮忙吗?非常感谢

尝试 xpath //*[@id='lhkexw-quoteequities']/div[2]/div[3]/div[2]/div/div[2]/div[1]/div[1]/span[2]/span

实际上你用双引号 (") 将 xpath 括起来。所以在 xpath 中你应该使用 single-quote (') for @id=''.