Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - Chromedriver, Selenium - Automate downloads

I am using Selenium 2.43.0 with Python 2.7.5. At one point, the test clicks on a button which sends form information to the server. If the request is successful, the server responds with

1) A successful message

2) A PDF with the form information merged in

I don't care to test the PDF, my test is just looking for a successful message. However the PDF is part of the package response from the server that I, as the tester, cannot change.

Until recently, this was never an issue using Chromedriver, since Chrome would automatically download pdfs into its default folder.

However, a few days ago one of my test environments started popping a separate window with a "Print" screen for the pdf, which derails my tests.

I don't want or need this dialog. How do I suppress this dialog programmatically using chromedriver's options? (Something equivalent to FireFox's pdfjs.disable option in about:config).

Here is my current attempt to bypass the dialog, which does not work (by "not work" does not disable or suppress the print pdf dialog window):

    dc = DesiredCapabilities.CHROME
    dc['loggingPrefs'] = {'browser': 'ALL'}

    chrome_profile = webdriver.ChromeOptions()
    profile = {"download.default_directory": "C:\SeleniumTests\PDF",
               "download.prompt_for_download": False,
               "download.directory_upgrade": True}
    chrome_profile.add_experimental_option("prefs", profile)
    chrome_profile.add_argument("--disable-extensions")
    chrome_profile.add_argument("--disable-print-preview")

    self.driver = webdriver.Chrome(executable_path="C:\SeleniumTests\chromedriver.exe",
                                       chrome_options=chrome_profile,
                                       service_args=["--log-path=C:\SeleniumTests\chromedriver.log"],
                                       desired_capabilities=dc)

All component versions are the same in both testing environments:

Selenium 2.43.0, Python 2.7.5, Chromedriver 2.12, Chrome (browser) 38.0.02125.122

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had to dig into the source code on this one - I couldn't find any docs listing the full set of Chrome User Preferences.

The key is "plugins.plugins_disabled": ["Chrome PDF Viewer"]}

FULL CODE:

dc = DesiredCapabilities.CHROME
dc['loggingPrefs'] = {'browser': 'ALL'}

chrome_profile = webdriver.ChromeOptions()
profile = {"download.default_directory": "C:\SeleniumTests\PDF",
           "download.prompt_for_download": False,
           "download.directory_upgrade": True,
           "plugins.plugins_disabled": ["Chrome PDF Viewer"]}
chrome_profile.add_experimental_option("prefs", profile)

#Helpful command line switches
# http://peter.sh/experiments/chromium-command-line-switches/
chrome_profile.add_argument("--disable-extensions")

self.driver = webdriver.Chrome(executable_path="C:\SeleniumTests\chromedriver.exe",
                               chrome_options=chrome_profile,
                               service_args=["--log-path=C:\SeleniumTests\chromedriver.log"],
                               desired_capabilities=dc)

Interestingly the blanket command chrome_profile.add_argument("--disable-plugins") switch did not solve this problem. But I prefer the more surgical approach anyways.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...