Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中的get请求中捕获URL_Python_Python 3.x - Fatal编程技术网

在Python中的get请求中捕获URL

在Python中的get请求中捕获URL,python,python-3.x,Python,Python 3.x,我正在编写python代码来自动测试下载文件。 我已通过单击下载图标下载了该文件。我现在尝试的是断言正确的文件是在后台下载的。为此,我需要在中提取get请求和参数。此参数和URL将包含用于标识是否下载了正确文档的值 你能帮帮我吗。非常感谢 下面是我正在运行的代码 import os from selenium import webdriver from selenium.webdriver.common.keys import Keys mime_types = "application/pd

我正在编写python代码来自动测试下载文件。 我已通过单击下载图标下载了该文件。我现在尝试的是断言正确的文件是在后台下载的。为此,我需要在中提取get请求和参数。此参数和URL将包含用于标识是否下载了正确文档的值

你能帮帮我吗。非常感谢

下面是我正在运行的代码

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

mime_types = "application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.adobe.xdp+xml"

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", mime_types)
fp.set_preference("plugin.disable_full_page_plugin_for_types", mime_types)
fp.set_preference("pdfjs.disabled", True)


driver = webdriver.Firefox(firefox_profile=fp)
driver.get('site-name')

# login
driver.find_element_by_class_name('textboxUserName').send_keys(name)
driver.find_element_by_id('dummy1').click()
driver.find_element_by_id('content__login_Password').send_keys(pwd)
driver.find_element_by_id('content__login_Password').send_keys(Keys.RETURN)

# search
driver.find_element_by_id('top-search-input').send_keys('9001')
driver.find_element_by_id('top-search-input').send_keys(Keys.RETURN)

# download
driver.find_element_by_xpath("//ul[@class='search-result-list']/li[1]/div/ul/li[7]").click()
如前所述,我希望确保该文件已下载。所以我想在点击下载按钮时从get中获取某些值


谢谢

我认为有两种方法可以做到这一点

  • 正如我在评论中提到的,您可以简单地检查下载链接的“href”属性,而不是单击它

  • 程序单击链接后,请使用:

    司机:为了安全起见,我暗暗地等待

    打印driver.current\u url

  • 希望这有帮助

    更新: 根据您的评论,如果AJAX最终打开了另一个窗口,那么这将实现以下目的:

    for handle in driver.window_handles:
        driver.switch_to_window(handle)
        print driver.current_url
    

    您当前下载的代码在哪里?如果您想要url,那么为什么不点击下载按钮,只需获取相关链接即可。如果这没有帮助,请提供更多信息。不幸的是,没有href属性。我相信它是由Ajax实现的。此外,该代码还绕过了用于下载文件的本机浏览器保存对话框,而是将其保存在预定义的位置。然后重试此操作。经过大约一个小时的研究,这是我能找到的最好的了。