本文整理汇总了C#中HTMLDocument.getElementsByName方法的典型用法代码示例。如果您正苦于以下问题:C# HTMLDocument.getElementsByName方法的具体用法?C# HTMLDocument.getElementsByName怎么用?C# HTMLDocument.getElementsByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTMLDocument
的用法示例。
在下文中一共展示了HTMLDocument.getElementsByName方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindElement
IHTMLElement FindElement(string xPath, HTMLDocument doc)
{
IHTMLElement theElement = null;
try
{
string[] tmp = xPath.Split(new string[] { "##" }, StringSplitOptions.None);
string elementAttributes = tmp[1];
string[] attributes = elementAttributes.Split(new string[] { ";" }, StringSplitOptions.None);
string attributeId = attributes[0].Replace(@"id=", "");
string attributeName = attributes[1].Replace(@"name=", "");
string attributeClass = attributes[2].Replace(@"class=", "");
string[] xPathParts = tmp[0].Split('/');
string elementTagName = xPathParts[xPathParts.Length - 1];
if (elementTagName.Contains("["))
{
elementTagName = elementTagName.Split('[')[0];
}
// try in first place the id (if it's unique)
theElement = doc.getElementById(attributeId);
if (theElement != null && !String.IsNullOrEmpty(attributeId))
{
int c = 0;
IHTMLElementCollection possibleElements = doc.getElementsByTagName(elementTagName);
foreach (IHTMLElement possibleElement in possibleElements)
{
if (possibleElement.id == attributeId)
{
c++;
}
}
if (c > 1)
{
theElement = null;
}
}
if (theElement == null && !String.IsNullOrEmpty(attributeName))
{
IHTMLElementCollection possibleElements = doc.getElementsByName(attributeName);
if (possibleElements.length == 1)
{
theElement = (IHTMLElement)possibleElements.item(null, 0);
}
}
// try next, the exact xpath
try
{
if (theElement == null)
{
IHTMLElementCollection possibleElements = doc.getElementsByTagName(elementTagName);
foreach (IHTMLElement possibleElement in possibleElements)
{
string possibleXPath = "";
try
{
possibleXPath = Utils.FindXPath(possibleElement);
//Utils.l(possibleXPath);
}
catch (Exception e) {
//Utils.l(e);
}
if (possibleXPath == xPath)
{
theElement = possibleElement;
break;
}
}
}
}
catch (Exception ex)
{
Utils.l(ex);
}
try
{
// next, try the path skipping attributes
if (theElement == null)
{
string cleanXPath = tmp[0];
IHTMLElementCollection possibleElements = doc.getElementsByTagName(elementTagName);
foreach (IHTMLElement possibleElement in possibleElements)
{
if (possibleElement.tagName == "INPUT") {
IHTMLInputElement tmpInput = (IHTMLInputElement)possibleElement;
if (tmpInput.type == "hidden" || tmpInput.type == "text" || tmpInput.type == "password")
{
continue;
}
}
string possibleXPath = Utils.FindXPath(possibleElement);
string[] possibleTmp = possibleXPath.Split(new string[] { "##" }, StringSplitOptions.None);
//.........这里部分代码省略.........