C# 无法从C中的selenium捕获异常#
我已经编写了一些代码来处理C#反射和selenium,以自动化URL的构建过程 但我无法捕捉到例外情况。我所做的是从selenium IDE导出为.html格式。解析后,它会自动从c#code调用与其相关的函数 但我没能抓住它。在这方面我需要帮助吗?任何猜测都无法捕获异常的原因 我使用的是Visual Studio Microsoft Visual C#2010 Express 代码如下C# 无法从C中的selenium捕获异常#,c#,selenium,C#,Selenium,我已经编写了一些代码来处理C#反射和selenium,以自动化URL的构建过程 但我无法捕捉到例外情况。我所做的是从selenium IDE导出为.html格式。解析后,它会自动从c#code调用与其相关的函数 但我没能抓住它。在这方面我需要帮助吗?任何猜测都无法捕获异常的原因 我使用的是Visual Studio Microsoft Visual C#2010 Express 代码如下 using System; using System.Text; using Sys
using System;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Selenium;
using System.Reflection;
using System.IO;
namespace SeleniumTests
{
public class Program
{
public ISelenium selenium;
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "URL");
selenium.Start();
}
//[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
}
}
public void myFun(string file)
{
bool flag = false;
string targetString = "", valueString = "", commandString = "";
string subString1, subString2;
HtmlAgilityPack.HtmlNode commandNode=null;
HtmlAgilityPack.HtmlNode targetNode=null;
HtmlAgilityPack.HtmlNode valueNode=null;
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(file);
doc.OptionCheckSyntax = true;
doc.OptionFixNestedTags = true;
doc.OptionAutoCloseOnEnd = true;
doc.OptionOutputAsXml = true;
doc.OptionDefaultStreamEncoding = Encoding.Default;
HtmlAgilityPack.HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
foreach (var row in table.SelectNodes("//tr"))
{
commandNode = row.SelectSingleNode("td[1]");
commandString = commandNode.InnerHtml.ToString();
subString1 = commandString.Substring(0, 1);
subString1 = subString1.ToUpper();
subString2 = commandString.Substring(1, commandString.Length - 1);
commandString = subString1 + subString2;
targetNode = row.SelectSingleNode("td[2]");
if (targetNode != null)
{
targetString = targetNode.InnerHtml.ToString();
if (targetString.Length == 0)
{
targetNode = null;
}
}
valueNode = row.SelectSingleNode("td[3]");
if (valueNode != null)
{
valueString = valueNode.InnerHtml.ToString();
if (valueString.Length == 0)
{
valueNode = null;
}
}
MethodInfo SeleniumMethod = typeof(ISelenium).GetMethod(commandString);
if (SeleniumMethod == null)
{
// Console.WriteLine(" \n NULL " + commandString);
continue;
}
if (targetNode == null && valueNode == null)
continue;
if (targetNode != null && valueNode != null)
{
String[] SeleniumArgs = new String[2];
SeleniumArgs[0] = targetNode.InnerHtml.ToString();
SeleniumArgs[1] = valueNode.InnerHtml.ToString();
try
{
SeleniumMethod.Invoke(selenium, SeleniumArgs);
}
catch (System.Reflection.TargetInvocationException)
{
}
catch (Selenium.SeleniumException se)
{
flag = true;
string lines = "\n Selenium Exception: Caught an exception while executing the script : " + file + " with the command : " + commandNode.InnerHtml.ToString() + " and the XPath is: " + targetNode.InnerHtml.ToString() + " and the value is : " + valueNode.InnerHtml.ToString() + " and the exception is as follows : ";
using (StreamWriter writer = new StreamWriter("Log.txt", true))
{
writer.WriteLine(lines);
writer.Flush();
writer.Close();
}
}
catch (Exception e)
{
flag = true;
string lines = "\n Exception: Caught an exception while executing the script : " + file + " with the command : " + commandNode.InnerHtml.ToString() + " and the XPath is: " + targetNode.InnerHtml.ToString() + " and the value is : " + valueNode.InnerHtml.ToString() + " and the exception is as follows : ";
using (StreamWriter writer = new StreamWriter("Log.txt", true))
{
writer.WriteLine(lines);
writer.Flush();
writer.Close();
}
}
}
else if (targetNode != null && valueNode == null)
{
String[] SeleniumArgs = new String[1];
SeleniumArgs[0] = targetNode.InnerHtml.ToString();
SeleniumMethod.Invoke(selenium, SeleniumArgs);
}
else if (valueNode != null)
{
String[] SeleniumArgs = new String[1];
SeleniumArgs[0] = valueNode.InnerHtml.ToString();
SeleniumMethod.Invoke(selenium, SeleniumArgs);
}
}// end of for
string line = "\n Script executed successfully ";
if (flag == false)
{
using (StreamWriter writer = new StreamWriter("Log.txt", true))
{
writer.WriteLine(line);
writer.Flush();
writer.Close();
}
}
}
}
public class TestProgram
{
static void Main(string[] args)
{
try
{
Program p = new Program();
p.SetupTest();
string file = @"1.html";
p.myFun(file);
p.TeardownTest();
}
catch { }
}
}
}
如果您试图在
Main()
方法中捕获异常,则需要在myFun
方法中冒泡异常。目前,您正在淹没myFun
方法中的任何异常
e、 g
你希望抓住哪一个例外?您已经包含了很多catch块,一些是空块,一些是陷阱异常。但是你说你不能捕获…那么你的目标是哪一个呢?你能告诉我你希望捕获哪一个异常块吗。或者在主方法的开始处设置一个断点,然后逐步遍历代码。查看抛出错误的位置。它显示:-Selenium.SeleniumException未被用户代码消息处理=错误:未找到标签为“ApplicationLoad”的选项Source=ThoughtWorks.Selenium.Core StackTrace:位于Selenium.HttpCommandProcessor.DoCommand(字符串命令,字符串[]args)在c:\Projects\WebDriver\trunk\dotnet\src\Selenium.Core\HttpCommandProcessor.cs中:Selenium.DefaultSelenium.Select(String selectLocator,String optionLocator)的第98行在c:\Projects\WebDriver\trunk\dotnet\src\Selenium.Core\DefaultSelenium.cs中:第567行InnerException:这表明问题出在
SetupTest()
方法上。尝试在SetupTest()
方法中显式处理Selenium.SeleniumException。
try
{
SeleniumMethod.Invoke(selenium, SeleniumArgs);
}
catch (System.Reflection.TargetInvocationException)
{
throw; //make this bubble up to the calling method.
}
catch (Selenium.SeleniumException se)
{
flag = true;
string lines = "\n Selenium Exception: Caught an exception while executing the script : " + file + " with the command : " + commandNode.InnerHtml.ToString() + " and the XPath is: " + targetNode.InnerHtml.ToString() + " and the value is : " + valueNode.InnerHtml.ToString() + " and the exception is as follows : ";
using (StreamWriter writer = new StreamWriter("Log.txt", true))
{
writer.WriteLine(lines);
writer.Flush();
writer.Close();
}
throw se; //bubble up to calling method
}
//etc...