selenium Selify+python下载一首歌曲后,Selify打开好莱坞投注页面,并粘在那里让下载后的代码不再运行

axzmvihb  于 2022-11-10  发布在  Python
关注(0)|答案(1)|浏览(110)

这是我的vscode终端的代码和输出,代码挂起是因为在下载一首歌曲后打开的Hollywood bet页面上出现了chromeDriver。我正在使用Mudopy+Selify从YouTube获取视频,使用转换器将其转换为mp3,然后下载歌曲。歌曲下载成功,但驱动程序没有退出它打开其他我没有请求的页面,然后被困在那里enter image description here

def download(song, artist = None, play_after_downloading = True):
    """Downloads the video to given directory"""

    try:
        file=open("down_path.txt")
        down_path=file.read()
        down_path=down_path.strip()
        file.close()

    except:
        down_path = os.getcwd()

    print("It will not take more than 1 minute if your download speed is good")
    if artist:
        song=song+ ' by ' +artist
    video=song
    '''
    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), options=chrome_options)

'''
    chromeOptions=Options()
    chromeOptions.add_experimental_option("prefs",{"download.default_directory":down_path})
    chromeOptions.add_argument("--headless")
    chromeOptions.add_argument('--ignore-certificate-errors-spki-list')
    chromeOptions.add_argument('--ignore-ssl-errors')
    #chromeOptions.add_argument("--disable-dev-shm-usage")
    #chromeOptions.add_argument("--no-sandbox")
    #chromeOptions.add_argument("--disable-javascript")
    #service = Service(executable_path=ChromeDriverManager().install())

    #driver = webdriver.Chrome(service=service)
    #driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), options=chromeOptions)
    driver = webdriver.Chrome(ChromeDriverManager(path=os.getcwd()).install(),options=chromeOptions)
    wait=WebDriverWait(driver,3)
    presence = EC.presence_of_element_located   
    visible = EC.visibility_of_element_located
    driver.get("https://www.youtube.com/results?search_query=" + str(video))
    ads =True
    wait.until(visible((By.ID, "video-title")))
    try:
        driver.find_element(By.XPATH,"//span[contains(@class,'style-scope ytd-badge-supported-renderer') and text()='Ad']")
    except Exception as e:
        ads=False
    if ads:    
        vid = driver.find_element(By.ID,"video-title")
        vid.click()
    else:
        vid = driver.find_element(By.ID,"video-title")
        vid.click()
    #driver.find_element(By.ID,"video-title").click()
    print(driver.current_url)
    url=driver.current_url
    driver.get("https://ytmp3.cc/en13/")
    driver.maximize_window()   
    driver.find_element(By.XPATH,"//*[@id='mp3']").click()
    driver.find_element(By.XPATH,"//*[@id='input']").send_keys(url)
    driver.find_element(By.XPATH,"//*[@id='submit']").click()
    time.sleep(6)
    driver.find_element(By.XPATH,"//*[@id='buttons' or @id='download']/span").click()
    #driver.find_element(By.XPATH,'//*[@id="download_list"]/div[1]').click()
    print("Downloading")
    time.sleep(10)
    driver.close()
    old_lst = os.listdir(down_path)
    while True:
            new_lst = os.listdir(down_path)

            if new_lst != old_lst:
                song = set(new_lst) - set(old_lst)
                song = str(song)
                song = song.replace("{","")
                song = song.replace("}","")
                song = song.strip("'")
                #song = song.replace(" ","")                

                if Path(song).suffix == '.mp3':
                    driver.quit()
                    if play_after_downloading:
                        print("Song downloaded to :"+down_path)
                        #print("playing")
                        #os.startfile(down_path+"/"+song)
                        print(song)

                    return song
                    break
    #print(song)    
    print("Hello from the creator of Mudopy,Smit Parmar and Ankit Raj Mahapatra.Do report bug if any")
    print("set download path")
    print("mudopy.download_path(r'download path') #dont forget to use 'r' while set download path")
    file.close()
2vuwiymt

2vuwiymt1#

尝试使用driver.quit()而不是driver.close()
close()仅关闭当前选项卡,但Quit()关闭整个浏览器。
希望这能有所帮助

相关问题