C# 在我的自定义程序中,调用部分保持未完成状态,在html中没有id,只有value属性
请仔细阅读我的密码。现在我要做的是创建一个应用程序,将其发布在博客上。现在,它包含许多代码,但下面提供的代码是最后一部分,这部分工作不正常。到目前为止,代码中没有错误,但程序正在跳过foreach循环部分 页面HTML的一部分C# 在我的自定义程序中,调用部分保持未完成状态,在html中没有id,只有value属性,c#,html,browser,getelementbyid,tryinvokemember,C#,Html,Browser,Getelementbyid,Tryinvokemember,请仔细阅读我的密码。现在我要做的是创建一个应用程序,将其发布在博客上。现在,它包含许多代码,但下面提供的代码是最后一部分,这部分工作不正常。到目前为止,代码中没有错误,但程序正在跳过foreach循环部分 页面HTML的一部分 curElement.GetElementsByTagName(“innerText”)看起来不正确GetElementsByTagName查找具有给定名称的HTML元素(“标记”)。我猜您的意思是获取元素的innerText属性,如下所示: ctrlIdentity =
curElement.GetElementsByTagName(“innerText”)
看起来不正确GetElementsByTagName
查找具有给定名称的HTML元素(“标记”)。我猜您的意思是获取元素的innerText
属性,如下所示:
ctrlIdentity = Convert.ToString(curElement.GetProperty("innerText"));
谢谢但它并没有进入foreach循环。为什么?你的代码在运行吗?在foreach循环中设置断点,查看元素集合是否为空。如果是,那么HTML可能不包含任何
a
元素。元素集合也不为null。但是我做了elementcollection.count,它是0,但是正如您看到的HTML部分,它有一个a标记,该页面中有许多a标记。代码得到1个HTML 1头1体标记,这意味着该页面肯定正在加载…您应该验证该页面是否包含您认为它的功能。可能内容与您发布的内容不匹配。例如,a
元素可能是通过JavaScript生成的,您的代码可能在此之前处理页面。在调试时,应该检查实际的HTML。
theElementCollection = browser.Document.GetElementsByTagName("a");
foreach (HtmlElement curElement in theElementCollection)
{
ctrlIdentity = Convert.ToString(curElement.GetElementsByTagName("innerText"));
if (ctrlIdentity == null)
{
Console.WriteLine("You are not logged in. Log in again.");
goto LOGINAGAIN;
}
else if (ctrlIdentity == "Make \"Keyword (3)\" bold ")
{
browser.Document.GetElementById("project_title").SetAttribute("value", projectTitle);
browser.Document.GetElementById("article_title").SetAttribute("value", title);
browser.Document.GetElementById("article_content").SetAttribute("value", content);
browser.Document.GetElementById("article_tags").SetAttribute("value", tags);
browser.Document.GetElementById("article_url_1").SetAttribute("value", url);
browser.Document.GetElementById("article_keyword_1").SetAttribute("value", keywords);
browser.Document.GetElementById("article_url_2").SetAttribute("value", url2);
browser.Document.GetElementById("article_keyword_2").SetAttribute("value", keywords2);
browser.Document.GetElementById("article_url_3").SetAttribute("value", url3);
browser.Document.GetElementById("article_keyword_3").SetAttribute("value", keywords3);
if (curElement.GetAttribute("value").Equals("Submit"))
{
curElement.InvokeMember("click");
Console.WriteLine("Clicked................");
}
else
Console.WriteLine("Unable to click!!!................");
}
else
Console.WriteLine("ERROR!!!! Not working.");
}
ctrlIdentity = Convert.ToString(curElement.GetProperty("innerText"));