I am using selenium to do a click function on a hyperlink, which is loaded on a certain page. The script works for google chrome, but does not for phantomjs. Why is this not
Since PhantomJS
would never proceed with a download request, we need to download the file manually.
The idea here is to click the "Convert" button, wait for the "Download" link to appear, get the href attribute, containing the link to the generated mp3
file, and download it via urllib.urlretrieve():
import urllib
from urlparse import urljoin
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
base_url = 'http://www.youtube-mp3.org/'
driver = webdriver.PhantomJS()
driver.get("http://www.youtube-mp3.org/?e=t_exp&r=true#v=hC-T0rC6m7I")
# convert the video to mp3
driver.find_element_by_id('submit').click()
# wait for download link to appear
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "Download")))
link = element.get_attribute('href')
url = urljoin(base_url, link)
# download the song
urllib.urlretrieve(url, 'song.mp3')
driver.quit()
# enjoy the great song