Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 使用Serelium下载pdf避免python3中的下载弹出窗口_Python 3.x_Selenium_Firefox_Selenium Webdriver_Download - Fatal编程技术网

Python 3.x 使用Serelium下载pdf避免python3中的下载弹出窗口

Python 3.x 使用Serelium下载pdf避免python3中的下载弹出窗口,python-3.x,selenium,firefox,selenium-webdriver,download,Python 3.x,Selenium,Firefox,Selenium Webdriver,Download,对象: 自动化以下过程。 1.打开特定网页,在搜索框中填写信息,然后提交。 2.从搜索结果中单击第一个结果并下载PDF 完成的工作: 为了达到这个目标,我写了一个代码作为第一步。代码工作正常,但打开了下载弹出窗口。在我无法摆脱它之前,我无法进一步自动化这个过程。寻找了很多解决方案。但没有一个成功 例如,这个解决方案对我来说很难理解,我认为它更多地与Java有关,而不是Python。按照很多人的建议,我改变了火狐的形象。剂量匹配,但不完全相同。我没有试过,因为没什么区别。甚至说要改变fire fo

对象:

自动化以下过程。 1.打开特定网页,在搜索框中填写信息,然后提交。 2.从搜索结果中单击第一个结果并下载PDF

完成的工作:

为了达到这个目标,我写了一个代码作为第一步。代码工作正常,但打开了下载弹出窗口。在我无法摆脱它之前,我无法进一步自动化这个过程。寻找了很多解决方案。但没有一个成功

例如,这个解决方案对我来说很难理解,我认为它更多地与Java有关,而不是Python。按照很多人的建议,我改变了火狐的形象。剂量匹配,但不完全相同。我没有试过,因为没什么区别。甚至说要改变fire fox的个人资料,但那是行不通的

我的代码如下

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from time import sleep
import time
import wget
from wget import download
import os
#set firefox Profile
profile = webdriver.FirefoxProfile()

profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
#set variable driver to open firefox
driver = webdriver.Firefox(profile)
#set variable webpage to open the expected URL
webpage = r"https://documents.un.org/prod/ods.nsf/home.xsp"  # edit me
#set variable to enter in search box

searchterm = "A/HRC/41/23"  # edit me

#open the webpage with get command
driver.get(webpage)

#find the element "symbol", insert data and click submit.
symbolBox = driver.find_element_by_id("view:_id1:_id2:txtSymbol")
symbolBox.send_keys(searchterm)
submit = driver.find_element_by_id("view:_id1:_id2:btnRefine")
submit.click()
#list of search results open up and 1st occarance is clicked by coppying its id element
downloadPage = driver.find_element_by_id("view:_id1:_id2:cbMain:_id135:rptResults:0:linkURL")
downloadPage.click()
#change windiows. with sleep time
window_before = driver.window_handles[0]
window_after = driver.window_handles[1]

time.sleep(10)

driver.switch_to.window(window_after)

#the actual download of the pdf page
theDownload = driver.find_element_by_id("download")

theDownload.click()


请导游

“选择”弹出窗口不是一个不同的窗口/选项卡,它只是一个HTML弹出窗口。您可以这样说,因为如果右键单击对话框,您将看到正常的上下文菜单。您只需选择“语言”和“文件类型”,然后单击“下载选定内容”按钮。

您有获取窗口句柄的代码,例如
window\u。弹出的对话框不是一个新窗口(在浏览器中),它只是一个HTML对话框。您可以像对待页面上的所有其他HTML一样对待它。我假设这就是你所谓的“下载弹出窗口”,你无法处理。。。至少当我在网站上手动遵循你的代码时,我看到了这一点。同意。这是来自操作系统的HTML对话。问题是,出现此框询问我是保存pdf文件还是打开它。我想跳过此框并直接保存文件