Java 如何保存在没有url的浏览器窗口中弹出的.pdf文件?

Java 如何保存在没有url的浏览器窗口中弹出的.pdf文件?,java,javascript,selenium,Java,Javascript,Selenium,我使用的是仅在公司网站上可用的.pdf文件。我不知道有什么方法可以下载它们并存储在一个文件夹中 我单击以获取.pdf文件的链接具有以下源代码: <a href="javascript:propertiesView('documentName')"> 当我点击链接时,一个.pdf文件在新的浏览器窗口中弹出,没有url,也没有源代码。我假设没有办法直接操作.pdf,那么如何保存它,以便从文件夹中操作.pdf 谢谢只要告诉浏览器始终将PDF文件保存到磁盘上,您可能会很幸运(信用卡

我使用的是仅在公司网站上可用的
.pdf
文件。我不知道有什么方法可以下载它们并存储在一个文件夹中

我单击以获取
.pdf
文件的链接具有以下源代码:

  <a href="javascript:propertiesView('documentName')">

当我点击链接时,一个
.pdf
文件在新的浏览器窗口中弹出,没有url,也没有源代码。我假设没有办法直接操作
.pdf
,那么如何保存它,以便从文件夹中操作
.pdf


谢谢

只要告诉浏览器始终将PDF文件保存到磁盘上,您可能会很幸运(信用卡到):

如果这不起作用,您可能可以使用
switchTo()
方法迭代所有打开的窗口/选项卡。尝试类似的方法来了解您打开的窗口(信用证到):

但这需要知道窗口的URL,您可能可以使用我提到的第二种方法(
driver.switchTo()
)和
driver.getCurrentUrl()
)获取该URL

我认为没有办法直接操纵这个.pdf文件

没错。有了硒,你就不能

如何保存它以便从文件夹中操作.pdfs

实际上,我已经在我工作的回归系统中实现了这一点

我的第一步是根据
属性view()
构建一个Url。方法确实如此

在您的例子中,
propertiesView()
执行某种类型的
窗口。我猜是打开。因此,您的目标是提取它打开的Url,并使用连接来构造Url

一旦你找到了你的Url,剩下的就成了小菜一碟。只需将url下载到名为
/pdfs
的文件夹中。请参阅以了解如何做到这一点

它甚至可能需要调用该方法来解决。。由于我对您的测试系统一无所知,除非您发布代码,否则我很难给出代码答案

我要告诉您的一个提示是,如果您正在使用Selenium 1,请使用

String url =selenium.getEval("var url = something; url;");
获取url并将其放入java对象。
(如果使用selenium 2,请使用)

如果要在IE中使用selenium将PDF保存到硬盘,则需要使用pywinauto和selenium。我只是将此代码用于在浏览器中打开的PDF文件

//selenium imports
from pywinauto import application //pywinauto import

//write selenium code to open up pdf in the browser 
driver = webdriver.Ie("IEDriverServer.exe", capabilities = caps)

//this could be a get or driver.execute_script() to click a link
driver.get("link to pdf")

//save pdf
app = application.Application()

//get the ie window by the title of the application (assuming only one window is open here)
ie =  app.window_(title_re = ".*Internet Explorer.*")

//this line focuses on the pdf that is open in the browser
static = ie.Static

//focus on the pdf so we can access the internal controls
static.SetFocus()

//control + h shows the pdf bar, but you don't really need this step 
//for it to work. i just used it as a debug
static.TypeKeys("^H")

//open save file dialog
static.TypeKeys("+^S")

//tricky here because the save file dialog opens up as another app instance   
//which is how pywinauto sees it
app2 = application.Application()

//bind to the window by title - name of the dialog
save = app2.window_(title_re = ".*Save As.*")

//this is the name of the property where you type in the filename
//way to be undescriptive microsoft
file_name = save[u'FloatNotifySink']

//type in the file name
save.TypeKeys("hello")

//pause for a second - you don't have to do this
time.sleep(4)

//find and bind the save button
button = save[u'&SaveButton']

//click the save button
button.Click()

您能提供新打开的窗口的pdf代码吗?谢谢您的回复。我不知道你说的代码是什么意思?初始页面有以下链接:
当我单击它时,一个.pdf文件将在新的浏览器窗口中打开。我无法查看它的源代码。Selenium中是否有任何命令可以单击此链接并保存.pdf?谢谢你,阿尔普。然而,我有没有办法摆脱这个循环呢。因为我只有两扇开着的窗户。如何在不循环的情况下切换到()。swithTo(URL)会做这项工作吗?如果您知道URL,只需使用第三种可能性并使用
copyURLToFile()
@Alp PDF被视为二进制文件,不幸的是,这不起作用:(嘿,伙计。我只是想说我搜索了一段时间,这正是我所需要的。这需要放在一个更好的地方,现在它在java下。不确定谁能做到这一点,但无论如何,谢谢。
org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
String url =selenium.getEval("var url = something; url;");
//selenium imports
from pywinauto import application //pywinauto import

//write selenium code to open up pdf in the browser 
driver = webdriver.Ie("IEDriverServer.exe", capabilities = caps)

//this could be a get or driver.execute_script() to click a link
driver.get("link to pdf")

//save pdf
app = application.Application()

//get the ie window by the title of the application (assuming only one window is open here)
ie =  app.window_(title_re = ".*Internet Explorer.*")

//this line focuses on the pdf that is open in the browser
static = ie.Static

//focus on the pdf so we can access the internal controls
static.SetFocus()

//control + h shows the pdf bar, but you don't really need this step 
//for it to work. i just used it as a debug
static.TypeKeys("^H")

//open save file dialog
static.TypeKeys("+^S")

//tricky here because the save file dialog opens up as another app instance   
//which is how pywinauto sees it
app2 = application.Application()

//bind to the window by title - name of the dialog
save = app2.window_(title_re = ".*Save As.*")

//this is the name of the property where you type in the filename
//way to be undescriptive microsoft
file_name = save[u'FloatNotifySink']

//type in the file name
save.TypeKeys("hello")

//pause for a second - you don't have to do this
time.sleep(4)

//find and bind the save button
button = save[u'&SaveButton']

//click the save button
button.Click()