当我在输入变量中输入一个视频名称时,vidplayer()函数不会查找它,而是在youtub上随机播放一段视频

2024-04-25 20:02:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个gui界面,在这里我可以输入视频的标题,一旦我点击按钮,selenium函数(vidplayer)就会在youtube上查找视频并播放结果页面的第一个。 一切正常,除了当我点击按钮时,selenium功能在我的youtube主页上播放第一个视频,而不是寻找我之前选择的视频



    from selenium import webdriver
    from tkinter import *
    root=Tk()
    root.title('Youtube Video Player')
    text=Label(root,text="In order to save time and avoid procrastination.\nEnter the name of the video 
    you have in mind,and by the power of the gods it will play")
    text.pack()
    input=Entry()
    input.pack()
    a=str(input.get())
    #playing the video with the 'a' variable title on youtube
    def vidplayer():
        browser=webdriver.Chrome('C:\\Users\\lenovo\\Downloads\\chromedriver')
        browser.get('https://www.youtube.com/results?search_query='+a)
        video=browser.find_element_by_id('video-title')
        video.click()
    button=Button(text="Play Video", command=vidplayer)
    button.pack()
    root.mainloop()

Tags: thetextfromimportbrowserinput视频title
1条回答
网友
1楼 · 发布于 2024-04-25 20:02:45

问题是,您在创建entry小部件大约一毫秒后调用a.get(),远远早于用户有机会输入任何内容。你知道吗

解决方法很简单:在实际需要时获取值,而不是在以下时间之前:

def vidplayer():
    query = input.get()
    url = 'https://www.youtube.com/results?search_query='+query
    browser=webdriver.Chrome('C:\\Users\\lenovo\\Downloads\\chromedriver')
    browser.get(url)
    ...

相关问题 更多 >

    热门问题