为什么我的脚本在 driver.execute_script() 中没有给出输出?

问题描述 投票:0回答:0

我正在尝试在 moodle 中导航讲座视频(这是一个 yt vid)(导航已禁用)

我正在使用 selenium 自动导航到视频中的特定点以解决出现的突击测验(我打算使用不同的代码自动解决)

这就是我在做什么:

try:
    # Wait for the element with the xpath of video 
    video_stream = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div/div/div[1]/video'))
    )
    # read totalTime.js
    with open('totalTime.js', 'r') as file:
        script_tt = file.read()

    TotalTime = driver.execute_script(script_tt + "; return totalTime()")
    print(TotalTime)
except TimeoutException:
    print("video did not show up for measuring total time")
    exit()

这是 totalTime.js,用于查找 totalTime

的脚本
function totalTime(){
    const xpath = "/html/body/div/div/div[1]/video";

    const container = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

    return container.duration
}

totalTime 中的脚本在控制台中工作,前提是我在 yt 的 iframe 上有 ctrl + shift + c(我不知道如何描述它),本质上让控制台知道,这是我想让你找到的东西(如果没有我亲自做这件事,它是无能为力的) ).但是当我运行 selenium 代码时,它不会为 print(totalTime) 行返回任何值。我已经在这个 selenium 代码块之前切换到 yt 的 iframe,所以我认为它应该能够在运行 execute_script 时找到该元素。我尝试使用 get_property('duration') 但那也没有产生。

selenium-webdriver html5-video duration execute-script
© www.soinside.com 2019 - 2024. All rights reserved.