c# - 如何以编程方式控制 Firefox,最好使用 C# 应用程序?

标签 c# firefox

我正在寻找一个程序来打开一个 Firefox 实例,即包含我的各种登录信息的默认 Firefox 实例,然后简单地切换几个站点。我可以使用以下代码做到这一点:

System.Diagnostics.Process.Start("firefox.exe", "thisIsMyURL");

但是,我相信您大多都知道,这只会打开一个新的 Firefox 进程,并将给定的 URL 作为要打开的默认站点。为了做我想做的事,我基本上必须打开一个新的 Firefox 进程,在页面上做我需要做的事情,终止进程,然后对我需要的每个页面重复此操作。这不太理想。所以,我希望有人会知道一种通过 API 或库或其他方式以编程方式控制 Firefox 的方法。我在 Google 上进行了搜索,到目前为止只找到了过时的解决方案,这些解决方案并没有真正解决我的问题。

一如既往,感谢您的帮助!感谢您提供的一切。

最佳答案

您可以使用适用于 C# 的 Selenium WebDriver。

这是一个跨平台 API,允许您使用 Java、C# 等 API 控制各种浏览器。

附上带有 Selenium WebDriver 测试的 C# 代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Interactions.Internal;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.IE;
using NUnit.Framework;
using System.Text.RegularExpressions;

namespace sae_test
{   class Program
    {   private static string baseURL;
        private static StringBuilder verificationErrors;

        static void Main(string[] args)
        {   // test with firefox
            IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver();
            // test with IE
            //InternetExplorerOptions options = new InternetExplorerOptions();
            //options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
            //IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver(options);
            SetupTest();
            driver.Navigate().GoToUrl(baseURL + "Account/Login.aspx");
            IWebElement inputTextUser = driver.FindElement(By.Id("MainContent_LoginUser_UserName"));
            inputTextUser.Clear();
            driver.FindElement(By.Id("MainContent_LoginUser_UserName")).Clear();
            driver.FindElement(By.Id("MainContent_LoginUser_UserName")).SendKeys("usuario");
            driver.FindElement(By.Id("MainContent_LoginUser_Password")).Clear();
            driver.FindElement(By.Id("MainContent_LoginUser_Password")).SendKeys("123");
            driver.FindElement(By.Id("MainContent_LoginUser_LoginButton")).Click();
            driver.Navigate().GoToUrl(baseURL + "finanzas/consulta.aspx");
            // view combo element
            IWebElement comboBoxSistema = driver.FindElement(By.Id("MainContent_rcbSistema_Arrow"));
            //Then click when menu option is visible 
            comboBoxSistema.Click();
            System.Threading.Thread.Sleep(500);
            // container of elements systems combo
            IWebElement listaDesplegableComboSistemas = driver.FindElement(By.Id("MainContent_rcbSistema_DropDown"));
            listaDesplegableComboSistemas.FindElement(By.XPath("//li[text()='BOMBEO MECANICO']")).Click();
            System.Threading.Thread.Sleep(500);
            IWebElement comboBoxEquipo = driver.FindElement(By.Id("MainContent_rcbEquipo_Arrow"));
            //Then click when menu option is visible 
            comboBoxEquipo.Click();
            System.Threading.Thread.Sleep(500);
            // container of elements equipment combo
            IWebElement listaDesplegableComboEquipos = driver.FindElement(By.Id("MainContent_rcbEquipo_DropDown"));

            listaDesplegableComboEquipos.FindElement(By.XPath("//li[text()='MINI-V']")).Click();
            System.Threading.Thread.Sleep(500);

            driver.FindElement(By.Id("MainContent_Button1")).Click();            
            try
            {   Assert.AreEqual("BOMBEO MECANICO_22", driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_LabelSistema\"]")).Text);
            }
            catch (AssertionException e)
            {   verificationErrors.Append(e.Message);
            }
            // verify coin format $1,234,567.89 usd
            try
            {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelInversionInicial\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
            }
            catch (AssertionException e)
            {   verificationErrors.Append(e.Message);
            }
            try
            {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelCostoOpMantto\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
            }
            catch (AssertionException e)
            {   verificationErrors.Append(e.Message);
            }
            try
            {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelCostoEnergia\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
            }
            catch (AssertionException e)
            {   verificationErrors.Append(e.Message);
            }
            try
            {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelcostoUnitarioEnergia\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
            }
            catch (AssertionException e)
            {   verificationErrors.Append(e.Message);
            }
            // verify number format 1,234,567.89
            try
            {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelConsumo\"]")).Text, "((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})?"));
            }
            catch (AssertionException e)
            {   verificationErrors.Append(e.Message);
            }
            System.Console.WriteLine("errores: " + verificationErrors);
            System.Threading.Thread.Sleep(20000);
            driver.Quit();
        }

        public static void SetupTest()
        {   baseURL = "http://127.0.0.1:8081/ver.rel.1.2/";
            verificationErrors = new StringBuilder();
        }

        protected static void mouseOver(IWebDriver driver, IWebElement element)
        {   Actions builder = new Actions(driver);
            builder.MoveToElement(element);
            builder.Perform();
        }

        public static void highlightElement(IWebDriver driver, IWebElement element)
        {   for (int i = 0; i < 2; i++)
            {   IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
                js.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",
                        element, "color: yellow; border: 2px solid yellow;");
                js.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",
                        element, "");
            }
        }
    }
}

下载地址http://vidadigital.com.mx/publicacion/source/Program.cs

关于c# - 如何以编程方式控制 Firefox,最好使用 C# 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493958/

相关文章:

c# - 托管在启用了 IIS express ssl 上的 WCF

firefox - Firefox 可以显示文件的上传状态吗?

c# - 长期运行的 Windows 服务中的依赖注入(inject) - Composition root 是正确的想法吗?

c# - 更改 Kendo UI 上传小部件的文本

c# - 通过 WebClient 上传 JSON

c# - Xamarin 表格 : Setting app language independently from OS settings

javascript - 从 Firefox 的缓存中读取脚本标签的来源

html - margin-top 行为因浏览器而异

Firefox 开发工具在单击元素时突出显示 DOM 节点

javascript - Ajax仅在IE中工作