为什么我使用selenium抓取youtube直播评论时返回数据一直为空(注:评论在iframe元素下)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time
# 创建Firefox浏览器的Options对象
options = Options()
options.add_argument('--proxy-server=127.0.0.1:7890')
driver = webdriver.Firefox(options=options)
driver.get("about:preferences#general")
print("开始休眠")
time.sleep(30)
print("休眠结束") #手动检查代理是否配置正确
# 打开目标网页
driver.get("https://www.youtube.com/watch?v=xl0NMRAnqbA")
time.sleep(10)
# 等待iframe加载完成
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//iframe[@id='chatframe']")))
iframe_src = iframe.get_attribute("src")
# 切换到iframe
driver.switch_to.frame(iframe)
# 定位目标元素
message_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='message']")))
print(message_element)
# 获取元素文本
message_text = message_element.text
print("Message:", message_text)
# 切换回默认的上下文
driver.switch_to.default_content()
# 关闭浏览器
driver.quit()
执行结束后message_text值一直为空
可以帮我指出一下问题处在什么地方吗?