使用 Microsoft Edge 将数据发送到网站(从 Internet Explorer 切换)

Send data to a website using Microsoft Edge (switching from Internet Explorer)

我有这个用 C# 编写的现有 Windows 表单应用程序,它处理数据并将处理后的数据发送到网站。

它在 Internet Explorer 中运行良好,但我希望它也能在 Microsoft Edge 上运行。可以使用 Edge 吗?

这是我的代码。

   private void SendDataToSPC(SHDocVw.InternetExplorer ie, string strSPCData)
        {           
            mshtml.IHTMLDocument3 doc = ie.Document as mshtml.IHTMLDocument3;
            mshtml.IHTMLElementCollection txtBoxes = doc.getElementsByTagName("INPUT");
            string[] data = Regex.Split(strSPCData, "\r\n");
            int intCtr = 0;
            foreach (mshtml.IHTMLElement txtBox in txtBoxes)
            {
                if (txtBox.getAttribute("className") != null)
                {
                    if (txtBox.getAttribute("className").Equals("vcs_de_textbox") && intCtr < data.Length)
                    {
                        txtBox.setAttribute("value", data[intCtr]);
                        intCtr++;
                    }
                }
            }

            foreach (mshtml.IHTMLElement button in txtBoxes)
            {
                if (button.getAttribute("className") != null)
                {
                    if (button.getAttribute("className").Equals("vcs_de_saveButton")){
                        button.click();
                    }
                }
            }

            ((mshtml.HTMLDocument)doc).focus();
        }

更新

我目前正在使用 Selenium,我尝试使用 Edge 驱动程序 returns 我遇到了

的错误

Additional information: A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:58191/session. The status of the exception was ReceiveFailure, and the message was: The underlying connection was closed: An unexpected error occurred on a receive.

但是当我尝试使用 firefoxDriver 时它正在工作,但我需要使用现有的 Firefox 打开浏览器会话(现有打开的 Firefox)。

Selinium webdriver 3.141.0 
Microsoft Edge 41.16299.1480.0 - browser 
Selenium.webdriver.microsoftdrivere 17.17134.0
OS Windows 10

这是我的最新代码

  private void SendDataToSPC2(string strSPCData)
        {
            //Create the reference for our browser
            //System.setProperty("webdriver.edge.driver");
            //IWebDriver driver = new FirefoxDriver();

            //Navigate to google page
            //driver.Navigate().GoToUrl("http:www.google.com");

            //Find the Search text box UI Element
            //IWebElement element = driver.FindElement(By.Id("p1d1"));

            //Perform Ops
            //element.SendKeys("executeautomation");

            //Close the browser
           // driver.Close();
            IWebDriver edgeDriver = new EdgeDriver();
            edgeDriver.Navigate().GoToUrl("http://phgcubadm1ms023/spc/jsp/dataentry/vcsdataentry/vcsDataEntryMain.action");
            var txtBoxes = edgeDriver.FindElements(By.TagName("INPUT"));
            string[] data = Regex.Split(strSPCData, "\r\n");
            int intCtr = 0;
            foreach (IWebElement txtbox in txtBoxes)
            {
                if (txtbox.GetAttribute("className") != null)
                {
                    if (txtbox.GetAttribute("className").Equals("vcs_de_textbox") && intCtr < data.Length)
                    {
                        txtbox.SendKeys(data[intCtr]);
                        intCtr++;
                    }
                }
            }

            foreach (IWebElement button in txtBoxes)
            {
                if (button.GetAttribute("className") != null)
                {
                    if (button.GetAttribute("className").Equals("vcs_de_saveButton"))
                    {
                        button.Click();
                    }
                }
            }
        }

我检查了你的代码,看起来你正试图在你的代码中自动化 IE 浏览器。

您不能 运行 Edge 浏览器的相同代码。

我建议您尝试使用 Microsoft Web 驱动程序 使用 c# 代码自动化 MS Edge 浏览器。

这是一个代码示例:

using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System;

namespace EdgeDriverTests
{
    public class Program
    {
        /*
        * This assumes you have added MicrosoftWebDriver.exe to your System Path.
        * For help on adding an exe to your System Path, please see:
        * https://msdn.microsoft.com/en-us/library/office/ee537574(v=office.14).aspx
        */
        static void Main(string[] args)
        {
            /* You can find the latest version of Microsoft WebDriver here:
            * https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
            */
            var driver = new EdgeDriver();

            // Navigate to Bing
            driver.Url = "https://www.bing.com/";

            // Find the search box and query for webdriver
            var element = driver.FindElementById("sb_form_q");

            element.SendKeys("webdriver");
            element.SendKeys(Keys.Enter);

            Console.ReadLine();
            driver.Quit();
        }
    }
}

参考文献:

  1. Download web driver

  2. Bringing automated testing to Microsoft Edge through WebDriver

  3. WebDriver (EdgeHTML)

  4. WebDriver (Chromium)