Hello:
I found your repo., it seems to be a good one, so I wanted to give it a try.
For using the console app by following the example from here:
https://github.com/mahdibland/Selenium-Proxy-Authentication.Chrome/blob/main/SeleniumProxyAuthentication.Sample/Program.cs
It works.
However, I want to use this repo in WinForms app. So have done the following:
-
Create a WinForms app project targeting .NET 6.0 (X64 CPU) on Windows 10 (Version 21H2). My IDE is Visual Studio 2022.
-
Install NUGET packages:
PM> Install-Package Selenium.Support -Version 4.2.0
PM> Install-Package Selenium.WebDriver -Version 4.2.0
PM> Install-Package Selenium.WebDriver.ChromeDriver -Version 102.0.5005.6102
PM> Install-Package SeleniumProxyAuthentication.Chrome -Version 2.1.1
-
The following is my C# code trying to do the same as console app.
using OpenQA.Selenium.Chrome;
using SeleniumProxyAuthentication;
namespace SeleniumProxy1Form
{
public partial class Form1 : Form
{
private static readonly ChromeOptions Chrome_Options = new();
#pragma warning disable CA2211 // Non-constant fields should not be visible
public static ChromeDriver Proxy_Driver;
#pragma warning restore CA2211 // Non-constant fields should not be visible
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Chrome_Options.AddProxyAuthenticationExtension(new Proxy(ProxyProtocols.HTTP, "proxy_server:proxy_port:proxy_username:proxy_password"));));
}
private async void BTNUKProxy_Click(object sender, EventArgs e)
{
Proxy_Driver = new(Chrome_Options)
{
Url = "http://ip-api.com/json/"
};
await Task.Delay(5000);
Proxy_Driver.Navigate().GoToUrl("https://www.bing.com/");
Console.Beep();
}
}
}
But when I run my program, I always get this kind of error:
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=System.Private.CoreLib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
at Interop.User32.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SeleniumProxy1Form.Program.Main() in C:\Selenium\SeleniumProxy1Form\SeleniumProxy1Form\Program.cs:line 14
This exception was originally thrown at this call stack:
Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions)
Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long)
System.IO.Strategies.OSFileStreamStrategy.OSFileStreamStrategy(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long)
System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long)
System.IO.Strategies.FileStreamHelpers.ChooseStrategy(System.IO.FileStream, string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int, System.IO.FileOptions, long)
System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int, bool)
System.IO.Compression.ZipFileExtensions.DoCreateEntryFromFile(System.IO.Compression.ZipArchive, string, string, System.IO.Compression.CompressionLevel?)
System.IO.Compression.ZipFile.DoCreateFromDirectory(string, string, System.IO.Compression.CompressionLevel?, bool, System.Text.Encoding)
System.IO.Compression.ZipFile.CreateFromDirectory(string, string)
SeleniumProxyAuthentication.Utilities..cctor.AnonymousMethod__9_5(string, string)
...
[Call Stack Truncated]
Inner Exception 1:
IOException: The process cannot access the file 'C:\Users\John\AppData\Roaming\ProxyAuthCache\4.3.1.1796950237348\Details\manifest.json' because it is being used by another process.
By the way, the following C# code works for console app project.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace SeleniumProxyAuthentication.Sample
{
public class Program
{
private static readonly ChromeOptions Chrome_Options = new();
static void Main()
{
Chrome_Options.AddProxyAuthenticationExtension(new Proxy(ProxyProtocols.HTTP, "proxy_server:proxy_port:proxy_username:proxy_password"));));
IWebDriver driver = new ChromeDriver(Chrome_Options);
driver.Navigate().GoToUrl(new Uri("http://ip-api.com/json/"));
Console.WriteLine("Done!");
Console.Beep();
}
}
}
I was thinking that my console project is using the json file, but I stop the console project, and change different proxy, or I even reboot my PC. But I still get the same error, but the console app project always works.
Please let me know how I can fix this issue, why it happens only for WinForms app?
Thanks,