简体   繁体   English

从外部Chrome(即CMD或C#)调用Google Chrome浏览器导航

[英]Invoke Google Chrome Navigation from outside chrome, i.e. CMD or C#

Looking for a solution to invoke Navigation in Chrome from outside of chrome by external process. 寻找一种解决方案,以通过外部流程从Chrome外部调用Chrome中的Navigation。

We have legacy WinForm Software that needs to browse an Angular HTML5 app that requires Chrome to run. 我们拥有旧版WinForm软件,需要浏览需要运行Chrome的Angular HTML5应用。 (I have no control of this.) (我对此无能为力。)

Something along lines of: 类似于:

    Process process = new Process();
    process.StartInfo.FileName = 
      @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    process.StartInfo.Arguments = "google.com" + " --new-window";
    process.Start();

And then some way to hook into that process and have the SAME TAB perform navigation. 然后通过某种方式进入该过程,并让SAME TAB执行导航。

magic.navigateTab1(www.anothersite.com);

I quite assume its not easily possible, solution may not use Process etc.. Just, anyway to accomplish it from outside chrome? 我完全假设它不容易实现,解决方案可能不使用Process等。只是,无论如何要从外部chrome完成它?

Whatever solution can't require installing > ~5MB to C# code base.. Ideally, no installation is preferred. 无论哪种解决方案都不需要在C#代码库中安装>〜5MB的内存。理想情况下,不建议安装。

Edit 1: Perhaps using something like this.. 编辑1:也许使用这样的东西。
https://sites.google.com/a/chromium.org/chromedriver/ https://sites.google.com/a/chromium.org/chromedriver/

I used Selenium to achieve that. 我用硒来实现这一目标。

First you must add via Nugget the following packages to your solution 首先,您必须通过Nugget将以下软件包添加到解决方案中

Selenium.WebDriver
Selenium.Chrome.WebDriver;

Then reference the following namespaces 然后引用以下名称空间

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

Create the following property on your class root 在类根目录上创建以下属性

IWebDriver driver;

On your class constructor add the following code to create a new Chrome Window handled by Selenium 在您的类构造函数上,添加以下代码以创建由Selenium处理的新的Chrome窗口

driver = new ChromeDriver();

Then on your buttons add the following 然后在您的按钮上添加以下内容

// Switch the action target to the first tab opened on chrome instace handled by Selenium
driver.SwitchTo().Window(driver.WindowHandles.First());
// Go to a given URL
driver.Navigate().GoToUrl("http://www.yourURL.com.br");

Your code should be like that 您的代码应该像这样

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace configure
{
    public partial class Form1 : Form
    {
        IWebDriver driver;
        public Form1()
        {
            InitializeComponent();
            driver = new ChromeDriver();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            driver.SwitchTo().Window(driver.WindowHandles.First());
            driver.Navigate().GoToUrl("http://www.yourURL.com.br");
        }
    }
}

Hope this solve your problem 希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM