• <legend id='m3it9'><style id='m3it9'><dir id='m3it9'><q id='m3it9'></q></dir></style></legend>

      <tfoot id='m3it9'></tfoot>

    1. <small id='m3it9'></small><noframes id='m3it9'>

      <i id='m3it9'><tr id='m3it9'><dt id='m3it9'><q id='m3it9'><span id='m3it9'><b id='m3it9'><form id='m3it9'><ins id='m3it9'></ins><ul id='m3it9'></ul><sub id='m3it9'></sub></form><legend id='m3it9'></legend><bdo id='m3it9'><pre id='m3it9'><center id='m3it9'></center></pre></bdo></b><th id='m3it9'></th></span></q></dt></tr></i><div id='m3it9'><tfoot id='m3it9'></tfoot><dl id='m3it9'><fieldset id='m3it9'></fieldset></dl></div>

          <bdo id='m3it9'></bdo><ul id='m3it9'></ul>
      1. 使用 Selenium 通过 PhantomJS 中的超链接下载文件

        时间:2023-07-05

        <i id='H746H'><tr id='H746H'><dt id='H746H'><q id='H746H'><span id='H746H'><b id='H746H'><form id='H746H'><ins id='H746H'></ins><ul id='H746H'></ul><sub id='H746H'></sub></form><legend id='H746H'></legend><bdo id='H746H'><pre id='H746H'><center id='H746H'></center></pre></bdo></b><th id='H746H'></th></span></q></dt></tr></i><div id='H746H'><tfoot id='H746H'></tfoot><dl id='H746H'><fieldset id='H746H'></fieldset></dl></div>

        <small id='H746H'></small><noframes id='H746H'>

        <legend id='H746H'><style id='H746H'><dir id='H746H'><q id='H746H'></q></dir></style></legend>
        <tfoot id='H746H'></tfoot>
            <tbody id='H746H'></tbody>

              <bdo id='H746H'></bdo><ul id='H746H'></ul>

                  本文介绍了使用 Selenium 通过 PhantomJS 中的超链接下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 selenium 在某个页面上加载的超链接上执行点击功能.该脚本适用于谷歌浏览器,但不适用于 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.

                  这里的想法是点击转换"按钮,等待下载"链接出现,获取 href 属性,包含生成的 mp3 文件的链接,并通过 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 中的超链接下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如果我的鼻子测试失败,如何截取屏幕截图? 下一篇:Webdriver 错误:“不存在警报"在抛出 UnexpectedAlertPresentException

                  相关文章

                  1. <small id='MIiZR'></small><noframes id='MIiZR'>

                    1. <legend id='MIiZR'><style id='MIiZR'><dir id='MIiZR'><q id='MIiZR'></q></dir></style></legend>
                    2. <tfoot id='MIiZR'></tfoot>
                      <i id='MIiZR'><tr id='MIiZR'><dt id='MIiZR'><q id='MIiZR'><span id='MIiZR'><b id='MIiZR'><form id='MIiZR'><ins id='MIiZR'></ins><ul id='MIiZR'></ul><sub id='MIiZR'></sub></form><legend id='MIiZR'></legend><bdo id='MIiZR'><pre id='MIiZR'><center id='MIiZR'></center></pre></bdo></b><th id='MIiZR'></th></span></q></dt></tr></i><div id='MIiZR'><tfoot id='MIiZR'></tfoot><dl id='MIiZR'><fieldset id='MIiZR'></fieldset></dl></div>
                        <bdo id='MIiZR'></bdo><ul id='MIiZR'></ul>