如何直接在pdf中执行保存此页面?我知道selenium无法控制chrome对话框......还有另外一种方法吗?
答案 0 :(得分:0)
您可以直接将请求发送到不包含Selenium的URL,并获取包含PDF文件内容的字节数组。之后,您可以使用某些PDF库读取文件内容(看起来像ITextSharp很受欢迎)。
答案 1 :(得分:0)
在Chrome浏览器中,所有对话框弹出窗口都是html页面,因此您可以使用Selenium单击它们。 在您的情况下,您可以导航到页面,模拟按Ctrl + P键盘按钮,切换到打印对话框窗口,单击更改按钮以更改打印机,单击保存到PDF,单击保存按钮,然后单击"另存为&#34 ;框显示 - 模拟按Enter键按钮实际保存文件。 我不做C#,但这就是Java中的样子,事实上我已经测试了它,它确实有效:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_CONTROL);
// get current browser window handles and switch to window with handle that is last in the list
Set<String> windowHandles = driver.getWindowHandles();
for (String handle : windowHandles) {
driver.switchTo().window(handle);
}
driver.findElement(By.xpath("//button[contains(text(), 'Change')]")).click();
driver.findElement(By.xpath("//span[contains(text(), 'Save as PDF')]")).click();
driver.findElement(By.xpath("//button[contains(text(), 'Save')]")).click();
// you might need to add waiter here that waits a second, since script is too fast
// and needs to wait for save dialog box to be shown
robot.keyPress(KeyEvent.VK_ENTER);
答案 2 :(得分:0)
另一种保存方法是命令Chrome浏览器将其保存到磁盘,而不是打开页面。 以下是方法:
ChromeOptions chromeOptions = new ChromeOptions();
// this will make automatically download to the default folder.
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriver = new ChromeDriver(chromeDriverService, chromeOptions);
var downloadsPath = KnownFolders.Downloads.Path;
var generatedFilePngs = Directory.GetFiles(downloadsPath, string.Format("{0}*.pdf", "TheNameOfYourPDF"));
答案 3 :(得分:0)
以下代码将有助于在 Selenium c# 中将页面另存为 pdf。
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
protected void PDFconversion(ChromeDriver driver, string root, string rootTemp)
{
//Grid.Rows.Add(TxtBxName.Text, TxtBxAddress.Text);
try
{
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
Thread.Sleep(500);
js.ExecuteScript("setTimeout(function() { window.print(); }, 0);");
Thread.Sleep(500);
driver.SwitchTo().Window(driver.WindowHandles.Last());
Thread.Sleep(500);
string JSPath = "document.querySelector('body>print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('#destinationSettings').shadowRoot.querySelector('#destinationSelect').shadowRoot.querySelector('print-preview-settings-section:nth-child(9)>div>select>option:nth-child(3)')";
Thread.Sleep(500);
IWebElement PrintBtn = (IWebElement)js.ExecuteScript($"return {JSPath}");
Thread.Sleep(500);
PrintBtn.Click();
string JSPath1 = "document.querySelector('body>print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('print-preview-button-strip').shadowRoot.querySelector('cr-button.action-button')";
Thread.Sleep(1000);
IWebElement PrintBtn1 = (IWebElement)js.ExecuteScript($"return {JSPath1}");
PrintBtn1.Click();
Thread.Sleep(1000);
SendKeys.Send("{HOME}");
SendKeys.Send(rootTemp + "\\" + "result.pdf"); // Path
SendKeys.Send("{TAB}");
SendKeys.Send("{TAB}");
SendKeys.Send("{TAB}");
SendKeys.Send("{ENTER}");
Thread.Sleep(1000);
}
catch (Exception ex)
{
}
}