Selenium:无法下载文件

时间:2016-04-08 09:19:10

标签: python python-2.7 selenium selenium-webdriver youtube

我正在尝试使用this网站自动从Youtube下载mp3文件。

目的:

  • 访问下载网站
  • 粘贴Youtube链接
  • 点击“转换视频”
  • 等待下载链接显示,然后单击

当我使用browser.find_by_id('dl_link').click()时,我的代码执行时没有任何错误,但文件未下载。为什么?

当我使用browser.click_link_by_partial_text('下载')时,我收到如下错误。

这是我的代码:

from splinter.browser import Browser
import time

with Browser() as browser:
   browser.visit("http://www.youtube-mp3.org")
   browser.find_by_id('youtube-url').fill("https://www.youtube.com/watch?v=lgT1AidzRWM")
   browser.find_by_id('submit').click()

   if browser.is_element_present_by_id('dl_link'):
      time.sleep(2)
      browser.click_link_by_partial_text('Download')
      # browser.find_by_id('dl_link').click()
      print "Clicked Download"
      time.sleep(2)

以下是我遇到的错误:

Traceback (most recent call last):
  File "/Users/anon/Dropbox/Programs/Proj/splinter2.py", line 11, in <module>
    browser.click_link_by_partial_text('Download')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/__init__.py", line 332, in click_link_by_partial_text
    return self.find_link_by_partial_text(partial_text).first.click()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/__init__.py", line 539, in click
    self._element.click()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 74, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver@googlecode.com/components/command-processor.js:10092)
    at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
[Finished in 7.2s with exit code 1]
[shell_cmd: python -u "/Users/anon/Dropbox/Programs/Proj/splinter2.py"]
[dir: /Users/anon/Dropbox/Programs/Proj]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

我已经检查了相同的其他问题(错误:“元素当前不可见,因此可能无法与”selenuim“标题进行交互)但我仍然无法解决此问题,因为Download按钮在我的情况下可见。

非常感谢任何帮助

编辑:

每当我尝试使用browser = Browser('firefox', profile=profile)设置个人资料时,都会收到以下错误:

Traceback (most recent call last):
  File "/Users/adb/Dropbox/Programs/Proj/Youtube Playlist MP3/splinter2.py", line 11, in <module>
    browser = Browser('firefox', profile=profile)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/browser.py", line 63, in Browser
    return driver(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/firefox.py", line 23, in __init__
    firefox_profile = FirefoxProfile(profile)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 77, in __init__
    ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 171, in copytree
    names = os.listdir(src)
TypeError: coercing to Unicode: need string or buffer, FirefoxProfile found

1 个答案:

答案 0 :(得分:0)

  1. 当我使用browser.find_by_id('dl_link')。click()时,我的代码执行时没有任何错误但是browser.click_link_by_partial_text('Download')会抛出?
  2. 因为您与“静态”元素进行交互。

    当您点击“转换视频”按钮时,您将刷新DOM。但是你的驱动程序仍然适用于旧的DOM。这就是使用Answer

    找不到的原因
    1. 当它吵架时,不会下载该文件。为什么?
    2. 可能是因为有一个弹出窗口询问您是否要保存并执行它。 $sort