1

here is the code and the the output of my vscode terminal the code hangs up due to chromedriver gets stuck on hollywoodbet pages that opens after a song is downloaded. i am using mudopy + selenium to fetch a video from youtube convert it to mp3 using a converter then download the song .Songs download succesfully but the driver does not exit it opens other pages i did not request for then gets stuck there 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()

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • why don't you use a context manager `with` to read files – rioV8 Oct 30 '22 at 13:18
  • i got solution from a previous post here https://stackoverflow.com/questions/47692436/how-to-block-the-pop-up-windows-in-chrome-by-selenium thank you – Nkosi Ncube Oct 30 '22 at 13:22

1 Answers1

0

Try using driver.quit() instead of driver.close()
close() only closes the current tab, but quit() closes the entire browser.
Hope it will help

Eugeny Okulik
  • 1,331
  • 1
  • 5
  • 16