首页 > 解决方案 > 使用关键字单击网页上的链接

问题描述

我希望能够打开至尊的网站(www.supremenewyork.com),进入任何一个标签(衬衫、夹克等)并能够输入我要点击的衬衫的关键字,然后让我的程序点击它。

我想出了如何让它自动点击结帐并填写字段。这是我们将使用的随机衬衫的 html。

https://www.supremenewyork.com/shop/all/shirts

我想点击Houndstooth Flannel Zip Up Shirt红色的

我已经读到您需要使用HTMLAgilityPack才能输入inner text's关键字。但我认为你们可以以正确的方式发送给我。

这是HTML

<article><div class="inner-article"><a style="height:150px;" href="/shop/shirts/gsfge9btl/mkza7miyc"><img width="150" height="150" src="//assets.supremenewyork.com/160974/vi/eYFAEBVxJT0.jpg" alt="Eyfaebvxjt0"></a><h1><a class="name-link" href="/shop/shirts/gsfge9btl/mkza7miyc">Houndstooth Flannel Zip Up Shirt</a></h1><p><a class="name-link" href="/shop/shirts/gsfge9btl/mkza7miyc">Red</a></p></div></article>

这是我的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace Supreme
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("https://supremenewyork.com/shop");

    }

    private void button1_Click(object sender, EventArgs e)
    {

        webBrowser1.Navigate("https://www.supremenewyork.com/shop/accessories");

    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        foreach (HtmlElement btn in 
            webBrowser1.Document.GetElementsByTagName("input"))
        {
            if (btn.GetAttribute("className") == "button")
            {
                btn.InvokeMember("Click");
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate("https://supremenewyork.com/shop/all/shirts");


    }

    private void button3_Click(object sender, EventArgs e)
    {
        webBrowser1.GoBack();
    }

    private void button4_Click(object sender, EventArgs e)
    {
        foreach (HtmlElement btn in
            webBrowser1.Document.GetElementsByTagName("a"))
            {
                if (btn.GetAttribute("className") == "button checkout")
                {
                    btn.InvokeMember("Click");
                }
            }
    }
}

谢谢你!

标签: c#html

解决方案


推荐阅读