3

自分.pdfの会社の Web サイトでのみ利用可能なファイルを操作しています。それらをダウンロードして1つのフォルダーに保存する方法を知りません。

ファイルを取得するためにクリックしたリンクに.pdfは、次のソース コードがあります。

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

リンクをクリックする.pdfと、新しいブラウザ ウィンドウにファイルがポップアップ表示されます。URL もソース コードもありません。.pdfそれを直接操作する方法はないと思いますが、フォルダから操作するためにどのように保存でき.pdfsますか?

ありがとうございました

4

3 に答える 3

3

ブラウザに常に PDF ファイルをディスクに保存するように指示するだけで、うまくいくかもしれません ( Dirkの功績によるものです)。

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

それが機能しない場合は、switchTo()メソッドを使用して、開いているすべてのウィンドウ/タブを反復処理できる可能性があります。開いているウィンドウについての洞察を得るには、次のようなことを試してください ( Prashant Shuklaの功績によるものです)。

    public void getWindows() {
        Set<String> windows = driver.getWindowHandles();

        for (String window : windows) {
            driver.switchTo().window(window);
            System.out.println(driver.getTitle());
        }
    }

ファイルをダウンロードする非セレン ソリューションは、apache-commons ライブラリを使用することです ( Shengyuan Luへのクレジット)。

org.apache.commons.io.FileUtils.copyURLToFile(URL, File)

ただし、これには、ウィンドウの URL を知っている必要があります。これは、( driver.switchTo()) とdriver.getCurrentUrl().

于 2013-09-26T14:34:28.747 に答える
2

Selenium を使用して IE のハード ドライブに 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()
于 2016-08-22T02:51:26.297 に答える
2

その.pdfを直接操作する方法はないと思います

そのとおりです。Selenium ではできません。

フォルダから .pdf を操作するために、どのように保存できますか?

私は実際に、私が働いている回帰システムにこれとまったく同じことを実装しました。

私の最初のステップは、に基づいて URL を作成することでしたpropertiesView()。メソッドはしました。

あなたの場合、propertiesView()ある種のことwindow.openは私の推測です。したがって、あなたの目標は、それが開く URL を抽出し、連結を使用して URL を構築することです。

URL を見つけたら、あとは簡単です。という名前のフォルダに URL をダウンロードするだけ/pdfsです。その方法については、この質問を参照してください。

それを理解するためにそのメソッドを呼び出す必要さえあるかもしれません..私はあなたのテスト中のシステムについて無知であるため、投稿しない限り、コードの回答を提供することは困難です.

私がお伝えするヒントは、Selenium 1を使用している場合は、

String url =selenium.getEval("var url = something; url;");

URLを取得してJavaオブジェクトに取得します。(Selenium 2 を使用している場合は、JavaScriptExecutor#executeScriptを使用します)

于 2013-09-26T19:10:20.603 に答える