使用Selenium通过PhantomJS中的超链接下载文件 [英] Download file via hyperlink in PhantomJS using Selenium
问题描述
我正在使用Selenium对超链接执行单击功能,该超链接加载在特定页面上.该脚本适用于Google chrome,但不适用于phantomjs.为什么这不起作用?
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 working?
from selenium import webdriver
driver = webdriver.Chrome()
#driver = webdriver.PhantomJS(executable_path = "/Users/jameslemieux/PythonProjects/phantomjs-1.9.8-macosx/bin/phantomjs")
driver.get("http://www.youtube-mp3.org/?e=t_exp&r=true#v=hC-T0rC6m7I")
elem = driver.find_element_by_link_text('Download')
elem.click()
driver.save_screenshot('/Users/jameslemieux/Desktop/Misc./test_image.png')
driver.quit()
这在chrome中有效,但始终会打开一个新的chrome窗口来完成任务.我读到我应该使用phantomjs使它在后台运行,但是当我将驱动程序切换到phantomjs时,下载似乎没有完成.屏幕截图已抓取,并且确实在正确的页面上,并且肯定存在下载".所以
This works in chrome, but it always opens up a new chrome window to complete the task. I read that I should use phantomjs to have it run behind the scenes, however when i switch the drivers to phantomjs, the download does not seem to go through. The screenshot grabs, and it is indeed at the right page, and the 'Download' is definitely there. So the
elem.click()
没有执行应做的操作,或者正在单击,但是phantomjs不知道如何处理直接下载链接.请帮忙,我已经结束了几个小时了.
is not doing what it should, or it IS clicking, but phantomjs doesnt know how to deal with a direct download link. Please help, ive been at this for hours on end.
推荐答案
由于PhantomJS
永远不会进行下载请求,因此,我们需要手动下载文件.
Since PhantomJS
would never proceed with a download request, we need to download the file manually.
这里的想法是单击转换"按钮,等待下载"链接出现, urllib.urlretrieve()
:
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
这篇关于使用Selenium通过PhantomJS中的超链接下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!