当 C# 没有任何 ID 和名称时,如何单击 Web 表单上的元素(超链接)

发布于 2024-11-19 01:47:21 字数 1546 浏览 4 评论 0 原文

在过去的两周里,我一直被一个问题困扰。

我正在使用 C# 开发一些网络抓取工具,并且在我的应用程序中使用 WinForms WebBrowser 控件。我可以填写在浏览器中打开的 Web 表单并使用以下代码自动提交:

HtmlElement submitButton = document.GetElementById("Element_ID″);
submitButton.InvokeMember(“click”);

到目前为止,一切都很好,但问题是 Web 表单中还有一个我想要单击的元素也是,但是这个元素没有任何 id 或名称,所以我不知道如何单击这个元素。

请尽快帮助我,我的硕士论文需要它。

(我想单击给定网站中的下一页箭头按钮: <一href="http://www.gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqI LbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-PA/S91 2oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmc GjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW" rel="nofollow">http://www.gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeS ZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAG D8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/ S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTL qpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmc GjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW )

For the last two weeks I have been kind of stuck on a problem.

I am developing some web scrapers using C# and I am using a WinForms WebBrowser control in my application. I am able to fill up the web form which is opened in my browser and submit it automatically by using the following code:

HtmlElement submitButton = document.GetElementById("Element_ID″);
submitButton.InvokeMember(“click”);

So far everything is fine, but the problem is that there is one another element in the web form that I want to click too, but this element does not have any id or name so I don't know how to click this one.

Please help me as soon as possible I need it for my master thesis.

(I want to click the next page arrow button in the give website:
http://www.gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2024-11-26 01:47:21

我过去使用嵌入式网络浏览器编写了许多网络抓取工具,所以您来对地方了。

当元素没有名称时,您需要通过内容或另一个已命名的关联元素来查找它。

  • 在第一个实例中,我们编写了辅助方法来迭代层次结构,查找元素中的特定内容。
  • 对于第二个选项,您获取命名元素并为所需的子元素使用特定索引。
  • 两者的组合(查找特定的父级,然后查找具有正确内容的子级)

在您的特定示例网页中,下一页锚点的类类型为“下一个箭头”你可以搜索。

I've written many web-scrapers in the past using embedded WebBrowsers, so you've come to the right place.

When the element does not have a name you need to find it by either content, or another associated element that is named.

  • In the first instance we wrote helper methods to iterate the hierachy looking for a specific piece of content within an element.
  • For the second option you get the named element and use a specific index for the desired child.
  • A combination of both (find a specific parent then look for a child with the right content)

In your specific example webpage, the next page anchor has a class type of "arrow next" you can search for.

倒带 2024-11-26 01:47:21

你可以做

HtmlElement next_arrow =  document.GetElementsByTagName("a")
                               .Cast<HtmlElement>()
                               .Where(e => e.GetAttribute("class") == "arrow next")
                               .FirstOrDefault();
if (next_arrow != null)
{
     next_arrow.InvokeMember("click");
}

You could do

HtmlElement next_arrow =  document.GetElementsByTagName("a")
                               .Cast<HtmlElement>()
                               .Where(e => e.GetAttribute("class") == "arrow next")
                               .FirstOrDefault();
if (next_arrow != null)
{
     next_arrow.InvokeMember("click");
}
寄离 2024-11-26 01:47:21

这是一个技巧,不是通过 InvokeMember("click") 而只是“模拟点击” -

这是第一页的链接:

gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pb yXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31b H4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-PA/S912oY4qhWzyjJkLvPZV 4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-PADS0 suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW

如你所见 page=0;单击下一步,给出链接 -

gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU/DMAyFf00vmzLFdprE8gkmwTggEENcp3RNxxh0o-pMA8euJBlO1G0 -p-pvcf58zNwUzW-pDKyQalSmckExl6DqJpKnPCEuVbDayFUvBcEIFXgVu1Ws2nV6Xac-p Zn89X5xFwoed2MvQbmI73rf1eL4L3SakFFsJOBpnzcJbte9W4hSI-pQ/S912oY4qhWz5LDS C992Dl/QR60ahPki2OZKeNfCgiba18oicmLV8lTcoS8t6BJ8zsHMo3yEU1VE1D1ZmWm7Tt -psXxtNwCMmjS4BhJ7oDAY72WR5CH/MT0l1HQEVa46QDK2Z/JSTyhcdIAWrZeGy8/k7LJ5Y QBAAA-e&page=1&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW

现在page=1

等等...一般来说,点击下一个意味着page=(x+1),点击上一个意味着page=(x-1)< /强>。所以根据要求构建一个字符串。这解决了您的问题,但是还有一些其他数据也随查询字符串一起发送,您也必须将其附加到字符串中。

Here's a trick, not by InvokeMember("click") rather just "simulating the click" -

this is the link for the first page:

gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW

as you see page=0; clicking next, gives the link -

gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU/DMAyFf00vmzLFdprE8gkmwTggEENcp3RNxxh0o-pmA8euJBlO1G0-p-pvCf58zNwUzW-pDKyQalSmckExl6DqJpKnPCEuVbDaYFUvBcEIFXgVu1Ws2nV6Xac-pZn89X5xFwoed2MvQbmI73rf1eL4L3SakFFsJOBpnzcJbte9W4hSI-pQ/S912oY4qhWz5LDSC992Dl/QR60ahPki2OZKeNfCgiba18oicmLV8lTcoS8t6BJ8zsHMo3yEU1VE1D1ZmWm7Tt-psXxtNwCMmjS4BhJ7oDAy72WR5CH/MT0l1HQEVa46QDK2Z/JsTyhcdIAWrZeGy8/k7LJ5YQBAAA-e&page=1&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW

now page=1

and so on... in general clicking next means page=(x+1) clicking prev means page=(x-1). so build a string according the requirements. this addresses ur problem, however there are some other data also sent with querystring, that u have to append to the string as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文