C# 使用Microsoft Edge将数据发送到网站(从Internet Explorer切换)
我用C#编写了这个现有的Windows窗体应用程序,它处理数据并将处理后的数据发送到网站 它在internet explorer中运行良好,但我希望它也能在Microsoft Edge上运行。可以使用Edge吗 这是我的密码C# 使用Microsoft Edge将数据发送到网站(从Internet Explorer切换),c#,winforms,internet-explorer,microsoft-edge,C#,Winforms,Internet Explorer,Microsoft Edge,我用C#编写了这个现有的Windows窗体应用程序,它处理数据并将处理后的数据发送到网站 它在internet explorer中运行良好,但我希望它也能在Microsoft Edge上运行。可以使用Edge吗 这是我的密码 private void SendDataToSPC(SHDocVw.InternetExplorer ie, string strSPCData) { mshtml.IHTMLDocument3 doc
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();
}
这是我的最新代码
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();
}
}
}
}
private void SendDataToSPC2(字符串strSPCData)
{
//为我们的浏览器创建参考
//System.setProperty(“webdriver.edge.driver”);
//IWebDriver=新的FirefoxDriver();
//导航到谷歌页面
//driver.Navigate().gotour(“http:www.google.com”);
//查找搜索文本框UI元素
//IWebElement元素=driver.FindElement(By.Id(“p1d1”);
//执行行动
//元素SendKeys(“executeautomation”);
//关闭浏览器
//driver.Close();
IWebDriver edgeDriver=新edgeDriver();
edgeDriver.Navigate().GoTour(“http://phgcubadm1ms023/spc/jsp/dataentry/vcsdataentry/vcsDataEntryMain.action");
var txtboxs=edgeDriver.FindElements(按.TagName(“输入”));
string[]data=Regex.Split(strSPCData,“\r\n”);
int intCtr=0;
foreach(txtboxs中的IWebElement txtbox)
{
if(txtbox.GetAttribute(“className”)!=null)
{
if(txtbox.GetAttribute(“className”).Equals(“vcs_de_textbox”)&&intCtr
我检查了您的代码,看起来您正在尝试在代码中自动使用IE浏览器
不能为边缘浏览器运行相同的代码
我建议您尝试使用Microsoft Web driver使用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();
}
}
}
参考资料:
嗨,只是一个更新,我尝试使用selenium和firefox驱动程序,但它不会接受重新查询。因为我需要使用现有的OpenFirefox会话。这也是为什么我使用firefox,因为edge driver在我的网页上不起作用。此外,如果我导航到该网页,我需要先登录,然后单击5个以上的按钮。因此,如果只可能使用现有的OpenFirefox,您的评论就不清楚了。您需要提供有关尝试运行Edge web驱动程序的详细信息吗?您尝试运行哪个版本?您正在使用哪个操作系统版本?运行web驱动程序时是否出现错误?根据更新的信息,可能是配置问题导致了此错误。您的Edge版本是41.16299.1480.0。我建议您尝试使用web驱动程序版本16299进行测试。Ref:我还建议您使用最新版本更新Edge浏览器,并使用最新的web driver.ow。在客户端pc中,如果我的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();
}
}
}