下载pdf而不是通过Selenium Webdriver从excel vba在Chrome中打开新标签页

问题描述 投票:0回答:1

我正在尝试使用Selenium Webdriver从excel宏下载pdf文件。 pdf正在新标签页中打开,我想不通如何直接下载。我在chrome中设置了pdf下载设置,手动进行时工作正常。但是,通过自动化,默认情况下该设置将被关闭。

我们如何启用该设置以在由Selenium Webdriver创建的Chrome实例中直接下载?

    Sub Invoice_Download()

    Dim Obj As New WebDriver
    Dim L As Long

    Obj.Start "chrome", ""
    Obj.Get "https://companyportal.com/login/"
    Obj.FindElementById("user_email").SendKeys ("username")
    Obj.FindElementById("user_password").SendKeys ("password")
    Obj.FindElementById("submit_button").Click

    L = Thisworkbook.Sheets("Invoices").Range("A1").End(xlDown).Row
    Range("A1").Select
    For rowNo = 2 To L
        ActiveCell.Offset(1).Select
        'Clear existing number from search box
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/input[1]").Clear
            'Enter a new invoice number saved in the macors workbook.
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/input[1]").SendKeys (ThisWorkbook.Sheets("Invoices").Range("A" & rowNo).Value)
            'Click on searh button
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/div[1]/button[1]").Click
        'Click on the found invoice link
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]").Click
        'Click on download button
        Obj.FindElementByLinkText("Download PDF").Click
    Next 'Proceed to next invoice number
End Sub
excel-vba selenium-webdriver selenium-chromedriver
1个回答
0
投票

我有解决方案。发布答案。添加此行可解决此问题。 Obj.SetPreference "plugins.always_open_pdf_externally", True

    Sub Invoice_Download()

    Dim Obj As New WebDriver
    Dim L As Long

    Obj.SetPreference "plugins.always_open_pdf_externally", True
    Obj.Start "chrome", ""
    Obj.Get "https://companyportal.com/login/"
    Obj.FindElementById("user_email").SendKeys ("username")
    Obj.FindElementById("user_password").SendKeys ("password")
    Obj.FindElementById("submit_button").Click

    L = Thisworkbook.Sheets("Invoices").Range("A1").End(xlDown).Row
    Range("A1").Select
    For rowNo = 2 To L
        ActiveCell.Offset(1).Select
        'Clear existing number from search box
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/input[1]").Clear
            'Enter a new invoice number saved in the macors workbook.
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/input[1]").SendKeys (ThisWorkbook.Sheets("Invoices").Range("A" & rowNo).Value)
            'Click on searh button
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/div[1]/button[1]").Click
        'Click on the found invoice link
        Obj.FindElementByXPath("/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]").Click
        'Click on download button
        Obj.FindElementByLinkText("Download PDF").Click
    Next 'Proceed to next invoice number
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.