我必须处理打印对话框(在浏览器中单击ctrl-p时出现的对话框).我尝试过:
Alert printDialog = driver.switchTo().alert();
printDialog.dismiss();
Run Code Online (Sandbox Code Playgroud)
但它不起作用.另外,我无法抓住它的窗口把手,因为它不是一个窗口......
是否可以处理这些对象以及如何处理?
这是我正在使用的,用户代理可以成功设置,而下载首选项不能.
Windows 7,Chrome 26,Selenium-dotnet-2.31.2,chromedriver_win_26.0.1383.0
ChromeOptions chromeOptions = new ChromeOptions();
var prefs = new Dictionary<string, object> {
{ "download.default_directory", @"C:\code" },
{ "download.prompt_for_download", false }
};
chromeOptions.AddAdditionalCapability("chrome.prefs", prefs);
chromeOptions.AddArgument("--user-agent=" + "some safari agent");
var driver = new ChromeDriver(chromeOptions);
Run Code Online (Sandbox Code Playgroud)
取自chromedriver.log:
[1.201][FINE]: Initializing session with capabilities {
"browserName": "chrome",
"chrome.prefs": {
"download.default_directory": "C:\\code",
"download.prompt_for_download": false
},
"chrome.switches": [ "--user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version..." ],
"chromeOptions": {
"args": [ "--user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version..." …Run Code Online (Sandbox Code Playgroud) 已经尝试了我可以在 Internet 上找到的所有解决方案,以便能够打印在 Python 中在 Selenium 中打开的页面。然而,当打印弹出窗口出现时,一两秒钟后它就会消失,没有保存 PDF。
这是正在尝试的代码。基于此处的代码 - /sf/answers/3062649061/
使用 Mojave 10.14.5 在 Mac 上编码。
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException
import time
import json
options = Options()
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local"
}
],
"selectedDestinationId": "Save as PDF", …Run Code Online (Sandbox Code Playgroud) python selenium python-3.x selenium-chromedriver selenium-webdriver
我正在使用Selenium 2.43.0和Python 2.7.5.有一次,测试点击了一个按钮,该按钮将表单信息发送到服务器.如果请求成功,则服务器响应
1)成功的消息
2)合并了表单信息的PDF
我不关心测试PDF,我的测试只是寻找成功的消息.然而,PDF是服务器的包响应的一部分,作为测试者,我无法更改.
直到最近,使用Chromedriver这一直不是问题,因为Chrome会自动将pdfs下载到其默认文件夹中.
但是,几天前我的一个测试环境开始弹出一个单独的窗口,其中包含pdf的"打印"屏幕,这会破坏我的测试.
我不想要或不需要这个对话框.如何使用chromedriver的选项以编程方式禁止此对话框?(相当于FireFox pdfjs.disable选项的东西about:config).
这是我目前试图绕过对话框的尝试,该对话框不起作用("不工作"不禁用或禁止打印pdf对话框窗口):
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)
Run Code Online (Sandbox Code Playgroud)
两个测试环境中的所有组件版本都相同:
Selenium 2.43.0,Python 2.7.5,Chromedriver 2.12,Chrome(浏览器)38.0.02125.122