停靠在winform硒实例中,而不是cefsharp



我有一个问题似乎无法解决,我用谷歌搜索过,但同样的问题似乎也无法解决问题,或者至少我不理解。

我已经将cefsharp停靠到WinForms面板中,并且可以注入javascript将数据输入到表单字段中,但我更喜欢selenium命令。

我所要做的就是将Selenium浏览器停靠在Winforms应用程序中,就像我在cefsharp中所做的那样,但命令行控制台和浏览器独立加载,而不停靠在winform中。

我的代码:-在表单浏览器上加载。

private static ChromiumWebBrowser browser;
public FormBrowser(string[] sitesList, string mode, FormMain formMain, Project _project, Func<string, Tuple<string, string, string>> getUserPassEmail)
{
InitializeComponent();
bool proxyUsed = false;
CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
if (!Cef.IsInitialized) // Check before Initialized.
{
// Init CEF.
CefSettings settings = new CefSettings
{
CachePath = Path.Combine(Directory.GetCurrentDirectory(), @"cache"),
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36",
LogFile = @"logsbrowser.log",
WindowlessRenderingEnabled = true,
MultiThreadedMessageLoop = true,
BrowserSubprocessPath = Path.Combine(Directory.GetCurrentDirectory(), @"CefSharp.BrowserSubprocess.exe"),
LogSeverity = LogSeverity.Error
};
if (_project.Proxy != "ip:port")
{
settings.CefCommandLineArgs.Add("proxy-server", _project.Proxy);
proxyUsed = true;
}
Cef.EnableHighDPISupport();
Cef.Initialize(settings);
}
// Init CEF.
// Init Selenium.
ChromeOptions options = new ChromeOptions();
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.SuppressInitialDiagnosticInformation = true;
options.AddArgument("--log-level=3");
options.AddArgument("--start-maximized");
options.AddExcludedArgument("enable-automation");
options.AddAdditionalCapability("useAutomationExtension", false);
options.AddArgument("remote-debugging-port=9222");
options.BinaryLocation = Path.Combine(Directory.GetCurrentDirectory(), @"CefSharp.BrowserSubprocess.exe");
ChromeDriver _driver = new ChromeDriver(service, options)
{
Url = "https://www.google.com/"
};
// Init Selenium.
_sitesToProcess = sitesList; // Array of sites [ url1.com , url2.com ] etc.
_modeToUse = mode;           // Mode either [REG] or [LAP].
_formMain = formMain;        // Main UI object.
string _mode = _modeToUse == "MODE_REG" ? "-[REG]" : "-[LAP]";
try
{
browser = new ChromiumWebBrowser("https://www.google.com/");
string[] _macroCode = File.ReadAllLines(@"engines" + _sitesToProcess[0] + _mode);
InitializeChromeBrowserAsync(_mode, _macroCode, _sitesToProcess[0], _project, getUserPassEmail, proxyUsed);
}
catch (Exception ex)
{
Helpers.DebugLogging($"[{DateTime.Now}]-[{ex}]");
}
}

然后在InitializeChromeBrowserAsync

private async void InitializeChromeBrowserAsync(string _macroModeToUse, string[] _macroCode, string _site, Project _project, Func<string, Tuple<string, string, string>> getUserPassEmail, bool proxyUsed)
{
try
{
await ExcecuteMacroCode(_macroModeToUse, _macroCode, _site, _project, getUserPassEmail, proxyUsed);
}
catch (Exception ex)
{
Helpers.DebugLogging($"[{DateTime.Now}]-[{ex}]");
}
}

转到ExcecuteMacroCode

public async Task ExcecuteMacroCode(string _macroModeToUse, string[] _macroCode, string _site, Project _project, Func<string, Tuple<string, string, string>> _getUserPassEmail, bool _proxyUsed)
{
var theProxy = _proxyUsed == true ? _project.Proxy : "NO_PROXY";
Log.Add("Proxy used: " + theProxy);
try
{
if (_macroModeToUse == "-[REG]")
{
foreach (string _macroCodeFile in _macroCode)
{
string[] code = _macroCodeFile.Split('|');
// Sometimes a row might have 3 sections, account for this.
string theThirdValue = null;
if (code.Length > 2)
{
theThirdValue = code[2];
}
switch (code[0])
{
case "RJ_U":
string cleanUrl = code[1].Replace("{HOSTNAME}", _site);
browser.Load(cleanUrl);
panelBrowserMain.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
browserMain.Text = cleanUrl;
browser.FrameLoadEnd += Browser_FrameLoadEnd;
browser.AddressChanged += Browser_AddressChanged;
await Task.Delay(int.Parse(_formMain.ComboBoxPauseInSeconds.Text + "000"));
break;
}
}
}
}
catch (Exception ex)
{
Helpers.DebugLogging($"[{DateTime.Now}]-[{ex}]");
}
}

我已经删除了很多代码以使其尽可能地相关,这部分case "RJ_U":是我们通过cefsharp访问该URL的地方。

我想问的是,如何对接selenium浏览器实例,以便使用selenium命令?还是不可能?

在这个Java示例中:https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver

我不知道它是如何在winform中停靠的。如有任何帮助,我们将不胜感激。

尝试使用if-elseif语句而不是switch-case语句。Switch语句可能存在异步等待问题。

查看等待任务.delay&int.parse一次。

相关内容

  • 没有找到相关文章

最新更新