1

I was trying to download an audio file using selenium, python and Chrome browser from url: audio file. (Note: Please use headphones as the audio plays automatically and open the file using chrome browser).

But when I used Inspect element on the three dots that show the download option I couldn't find any way to reference that element in the code. So, is there any other way I can press that 'three dots' button and then press on 'download button'??

Below is the code I wrote for the performing the action.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys


s=Service(ChromeDriverManager().install())
options = webdriver.ChromeOptions()
preferences = {'download.default_directory': 'some_path'}
options.add_experimental_option('prefs', preferences)
driver = webdriver.Chrome(service=s, options=options)
driver.maximize_window()

driver.get('https://www.valmiki.iitk.ac.in/sites/default/files/audio/1-1-1.mp3')
action = ActionChains(driver)
dot_dot_dot = driver.find_element(By.XPATH, "//video[@name='media']")
dot_dot_dot.click().perform()
# remaining code to click on the download button
comp1810
  • 11
  • 5

1 Answers1

0

Basically, the “3 dots” option is a part of the controls of the video element and there is no way (that i know of, anyway) through which you could have a reference to that 3 dot option as an element, simply because it isn’t an element.

SOLUTION 1 (NOT RECOMMENDED):

Now, you could use Pyautogui to take the screenshot of the entire screen and to check where an image of the 3 dots is present (using the locateOnScreen method), and then to click on the location. The code would look something like:-

import pyautogui

location_of_3dots = pyautogui.locateOnScreen(image=“ImageOf3Dots.png”, grayscale=True, confidence=0.6)
x_pos = location_of_3dots.left + (location_of_3dots.width / 2)
y_pos = location_of_3dots.top + (location_of_3dots.height / 2)

pyautogui.click(x_pos, y_pos)

… and then repeat the same thing for the download option that pops up.

SOLUTION 2 (RECOMMENDED):

But really, if you only want to be downloading the file, that’s too much! If clicking on the 3 dots is not your priority, then you should definitely try out the below solution. Basically the code creates an anchor tag in the document with the attribute of download, and the source of the file set as the source of the video element on the page. Then the element is clicked and after the file gets downloaded, it is removed from DOM. The code would look something like this :-

driver.get('https://www.valmiki.iitk.ac.in/sites/default/files/audio/1-1-1.mp3')

# The code to download the file

driver.execute_script(‘’’
    // Javascript Code to create the anchor tag and download the file
    let aLink = document.createElement("a");
    let videoSrc = document.querySelector("video").firstChild.src;
    aLink.href = videoSrc;
    aLink.download = "";
    aLink.click();
    aLink.remove();
’’’)

# rest of your code…

On executing the code, a window appears to choose the download destination and the file name.

HerrAlvé
  • 587
  • 3
  • 17