C# Selenium Saving pdf page - c#

How can I perform the save this page directly in pdf? I know that selenium is not able to control the chrome dialog box ... is there another way?
Page to save in pdf:

This below code will help to save page as pdf in Selenium c#.
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)
{
}
}

Another way to save is by commanding to the Chrome to save to the disk instead opening to the Page.
Below is the way to do:
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"));

You can directly send the request to the url without Selenium involved and get the byte array with content of PDF file. After that you can read file content using some PDF library (looks like ITextSharp is popular).

Inside Chrome browser all the dialog popups are html pages so you can click on them using Selenium.
In your case you can navigate to a page, simulate Ctrl + P keyboard button press, switch to print dialog window, click Change button to change the printer, click Save to PDF, click Save button and when "Save As" box is shown - simulate Enter keyboard button press to actually save the file.
I don't do C#, but here is how that looks like in Java, in fact I have tested it and it actually works:
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);

Related

How to start Zoom meeting automatically from C# Console Application

I am trying to build a simple app (As Console Application) that enters a Zoom meeting automatically at a specified time.
The app opens the Zoom meeting using Process.Start function, and then wait for the "Zoom Meeting" process to start.
It works well if I use a Windows shortcut file (.lnk extension) with the correct parameters, like shown here
But is doesn't work when I use the "regular" Zoom link (the url) because it opens the browser and waits for user input (It shows an alert).
I know how to send input to a process, so all I need is to a reference to the browser window that opened, but I can't find it.
The Process.Start doesn't return it and when I looped through all processes (Process.GetProcesses) I couldn't find any useful name that I can search for.
So, how can I get a reference to the browser process? Or at least send it input when it start.
Thanks in advance.
=== EDIT ===
After digging in Windows Registry, I have found an even simpler code to achieve it:
public static void OpenZoomMeeting(string link)
{
string zoomDirectory = Environment.ExpandEnvironmentVariables(#"%APPDATA%\Zoom\bin");
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = $#"{zoomDirectory}\Zoom.exe",
Arguments = $"--url={link}",
WorkingDirectory = zoomDirectory
};
Process.Start(startInfo);
}
=== OLD CODE ===
Found the solution thanks to jdweng
He said that I should to use a WebBrowser to open the meeting without the prompt, so I looked into it.
Because my app is a Console Application, I can't just use a WebBrowser so I found That solution and it worked for me.
Thank you for your help
===The code===
private void RunBrowserThread(string url) {
var th = new Thread(() => {
var br = new WebBrowser();
br.DocumentCompleted += Browser_DocumentCompleted;
br.Navigate(url);
Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
var br = sender as WebBrowser;
if (br.Url == e.Url) {
Console.WriteLine("Natigated to {0}", e.Url);
Application.ExitThread(); // Stops the thread
}
}

Right click on button and Save PDF file As

There is a link behind button with PDF file on web page that I would like to access. PDF link has API key and can't be accessed directly. Button has ID company_report_link.
If I do reportDownloadButton.Click(); PDF gets opened in separate tab of browser but I can't figure out how to download it.
I have tried right click the button and select Save As? I am able to open Chrome Context menu with this one, but can't select Save link As..
// click the link to download
var reportDownloadButton = driver.FindElementById("company_report_link");
// reportDownloadButton.Click();
// if clicking does not work, get href attribute and call GoToUrl() -- this may trigger download
// var href = reportDownloadButton.GetAttribute("href");
// driver.Navigate().GoToUrl(href);
InputSimulator s = new InputSimulator();
Actions action1 = new Actions(driver);
action1.ContextClick(reportDownloadButton);
s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
Thread.Sleep(2000);
s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
Thread.Sleep(2000);
s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
Thread.Sleep(2000);
s.Keyboard.KeyPress(VirtualKeyCode.DOWN);
Thread.Sleep(2000);
s.Keyboard.KeyPress(VirtualKeyCode.RETURN);
I have tried to do the trick with WebClient
var reportDownloadButton = driver.FindElementById("company_report_link");
var text = reportDownloadButton.GetAttribute("href");
// driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
WebClient client = new WebClient();
// Save the file to desktop for debugging
var desktop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
string fileName = desktop + "\\myfile.pdf";
client.DownloadFile(text, fileName);
but it does not work because of API. Web site is giving:
System.Net.WebException: 'The remote server returned an error: (401)
Unauthorized.'

uwp close browser programmatically

I'm creating a simple news feed, it i want it can open the browser to show the details of the news, but i don't know how to close the browser , here is the code of how I open the browser,anyone can teach me how to off the browser using uwp? '
public async void test()
{
RootObject mynews = await NewsProxy.GetNews();
string website = mynews.articles[i].url;
var uriWeb = new Uri(websites);
var success = await Windows.System.Launcher.LaunchUriAsync(uriWeb);
if (success)
{
//Uri launched
}
else
{
// uri launch failed
}
}
var success = await Windows.System.Launcher.LaunchUriAsync(uriWeb);
Your code is actually telling the OS to Launch the given url and the OS in turn launches the default browser with the given URL. So you are not actually launching the browser.
In order to have full control over the browser behavior you can implement your own WebView and then use the url to navigate your WebView.
webView1.Navigate("http://www.contoso.com");
(MSDN Documentation for WebView)

Selenium Instance loses session data

Hi. I am trying to run the following code and test case, such that
The user is logged in
The browser is closed
The browser is started again.
Subsequently, code is executed to verify the session still exists.
public void test()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://site/login");
this.login();
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://site/homepage");
this.CheckSessionExist(); /// reutrn false as the session is lost
}
My question is: shouldn't I be able to retrieve the session after I initialize another driver instance? If this is not how it works, then how can I replicate this test case?
A suggestion:
public void test()
{
IWebDriver driver = new ChromeDriver(); // This opens a window
driver.Navigate().GoToRul("https://www.google.com"); // Navigate to a dummy url, this is to keep the chrome driver session alive later
IWebElement body = driver.FindElement(By.CssSelector("body"));
body.SendKeys(Keys.CONTROL + 't'); // This opens a new window
String winHandleBefore = driver.CurrentWindowHandle();
//Switch to new window opened
foreach(String winHandle in driver.WindowHandles{
if(winHandle !- winHandleBefore)
driver.SwitchTo().Window(winHandle);
}
driver.Navigate().GoToUrl("http://site/login");
this.login();
driver.Close(); // This closes the current window, but should keep the ChromeDriver session alive
driver.SwitchTo().Window(winHandleBefore);
IWebElement body = driver.FindElement(By.CssSelector("body"));
body.SendKeys(Keys.CONTROL + 't'); // This opens a new window
String winHandleBefore = driver.CurrentWindowHandle();
//Switch to new window opened
foreach(String winHandle in driver.WindowHandles{
if(winHandle !- winHandleBefore)
driver.SwitchTo().Window(winHandle);
}
driver.Navigate().GoToUrl("http://site/homepage");
this.CheckSessionExist(); /// reutrn false as the session is lost
}
The idea here is you're
Opening a dummy window to keep the current ChromeDriver session alive
Opening a new window that you will use to navigate to your login test
Switch to that new window, navigate to the url
Login
Close that window
Switch back to the original window with the dummy url
Open another new window
Navigate to the home page
Ensure your session is still alive
Probably overly complicated, and a better solution likely exists.
Extract the cookies before closing the browser, open new browser, navigate to same domain and drop cookies

How to open in default browser in C#

I am designing a small C# application and there is a web browser in it. I currently have all of my defaults on my computer say google chrome is my default browser, yet when I click a link in my application to open in a new window, it opens internet explorer. Is there any way to make these links open in the default browser instead? Or is there something wrong on my computer?
My problem is that I have a webbrowser in the application, so say you go to google and type in "stack overflow" and right click the first link and click "Open in new window" it opens in IE instead of Chrome. Is this something I have coded improperly, or is there a setting not correct on my computer
===EDIT===
This is really annoying. I am already aware that the browser is IE, but I had it working fine before. When I clicked a link it opened in chrome. I was using sharp develop to make the application at that time because I could not get c# express to start up. I did a fresh windows install and since I wasn't too far along in my application, I decided to start over, and now I am having this problem. That is why I am not sure if it is my computer or not. Why would IE start up the whole browser when a link is clicked rather than simply opening the new link in the default browser?
You can just write
System.Diagnostics.Process.Start("http://google.com");
EDIT: The WebBrowser control is an embedded copy of IE.
Therefore, any links inside of it will open in IE.
To change this behavior, you can handle the Navigating event.
For those finding this question in dotnet core. I found a solution here
Code:
private void OpenUrl(string url)
{
try
{
Process.Start(url);
}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}
After researching a lot I feel most of the given answer will not work with dotnet core.
1.System.Diagnostics.Process.Start("http://google.com"); -- Will not work with dotnet core
2.It will work but it will block the new window opening in case default browser is chrome
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
myProcess.Start();
Below is the simplest and will work in all the scenarios.
Process.Start("explorer", url);
public static void GoToSite(string url)
{
System.Diagnostics.Process.Start(url);
}
that should solve your problem
Did you try Processas mentioned here: http://msdn.microsoft.com/de-de/library/system.diagnostics.process.aspx?
You could use
Process myProcess = new Process();
try
{
// true is the default, but it is important not to set it to false
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
myProcess.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
My default browser is Google Chrome and the accepted answer is giving the following error:
The system cannot find the file specified.
I solved the problem and managed to open an URL with the default browser by using this code:
System.Diagnostics.Process.Start("explorer.exe", "http://google.com");
I'm using this in .NET 5, on Windows, with Windows Forms. It works even with other default browsers (such as Firefox):
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
Based on this and this.
Try this , old school way ;)
public static void openit(string x)
{
System.Diagnostics.Process.Start("cmd", "/C start" + " " + x);
}
using : openit("www.google.com");
Am I the only one too scared to call System.Diagnostics.Process.Start() on a string I just read off the internet?
public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
Request = request;
string url = Request.Url;
if (Request.TransitionType != TransitionType.LinkClicked)
{ // We are only changing the behavoir when someone clicks on a link.
// Let the embedded browser handle this request itself.
return false;
}
else
{ // The user clicked on a link. Something like a filter icon, which links to the help for that filter.
// We open a new window for that request. This window cannot change. It is running a JavaScript
// application that is talking with the C# main program.
Uri uri = new Uri(url);
try
{
switch (uri.Scheme)
{
case "http":
case "https":
{ // Stack overflow says that this next line is *the* way to open a URL in the
// default browser. I don't trust it. Seems like a potential security
// flaw to read a string from the network then run it from the shell. This
// way I'm at least verifying that it is an http request and will start a
// browser. The Uri object will also verify and sanitize the URL.
System.Diagnostics.Process.Start(uri.ToString());
break;
}
case "showdevtools":
{
WebBrowser.ShowDevTools();
break;
}
}
}
catch { }
// Tell the browser to cancel the navigation.
return true;
}
}
This code was designed to work with CefSharp, but should be easy to adapt.
Take a look at the GeckoFX control.
GeckoFX is an open-source component
which makes it easy to embed Mozilla
Gecko (Firefox) into any .NET Windows
Forms application. Written in clean,
fully commented C#, GeckoFX is the
perfect replacement for the default
Internet Explorer-based WebBrowser
control.
dotnet core throws an error if we use Process.Start(URL). The following code will work in dotnet core. You can add any browser instead of Chrome.
var processes = Process.GetProcessesByName("Chrome");
var path = processes.FirstOrDefault()?.MainModule?.FileName;
Process.Start(path, url);
This opened the default for me:
System.Diagnostics.Process.Start(e.LinkText.ToString());
I tried
System.Diagnostics.Process.Start("https://google.com");
which works for most of the cases but I run into an issue having a url which points to a file:
The system cannot find the file specified.
So, I tried this solution, which is working with a little modification:
System.Diagnostics.Process.Start("explorer.exe", $"\"{uri}\"");
Without wrapping the url with "", the explorer opens your document folder.
In UWP:
await Launcher.LaunchUriAsync(new Uri("http://google.com"));
Open dynamically
string addres= "Print/" + Id + ".htm";
System.Diagnostics.Process.Start(Path.Combine(Environment.CurrentDirectory, addres));
update the registry with current version of explorer
#"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
public enum BrowserEmulationVersion
{
Default = 0,
Version7 = 7000,
Version8 = 8000,
Version8Standards = 8888,
Version9 = 9000,
Version9Standards = 9999,
Version10 = 10000,
Version10Standards = 10001,
Version11 = 11000,
Version11Edge = 11001
}
key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
This works nicely for .NET 5 (Windows):
ProcessStartInfo psi = new ProcessStartInfo {
FileName = "cmd.exe",
Arguments = $ "/C start https://stackoverflow.com/",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true
};
Process.Start(psi);
to fix problem with Net 6
i used this code from ChromeLauncher
,default browser will be like it
internal static class ChromeLauncher
{
private const string ChromeAppKey = #"\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe";
private static string ChromeAppFileName
{
get
{
return (string) (Registry.GetValue("HKEY_LOCAL_MACHINE" + ChromeAppKey, "", null) ??
Registry.GetValue("HKEY_CURRENT_USER" + ChromeAppKey, "", null));
}
}
public static void OpenLink(string url)
{
string chromeAppFileName = ChromeAppFileName;
if (string.IsNullOrEmpty(chromeAppFileName))
{
throw new Exception("Could not find chrome.exe!");
}
Process.Start(chromeAppFileName, url);
}
}
I'd comment on one of the above answers, but I don't yet have the rep.
System.Diagnostics.Process.Start("explorer", "stackoverflow.com");
nearly works, unless the url has a query-string, in which case this code just opens a file explorer window. The key does seem to be the UseShellExecute flag, as given in Alex Vang's answer above (modulo other comments about launching random strings in web browsers).
You can open a link in default browser using cmd command start <link>, this method works for every language that has a function to execute a system command on cmd.exe.
This is the method I use for .NET 6 to execute a system command with redirecting the output & input, also pretty sure it will work on .NET 5 with some modifications.
using System.Diagnostics.Process cmd = new();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("start https://google.com");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();

Categories