通过使用关键字单击网页上的链接

时间:2018-11-20 19:05:00

标签: c# html

我希望能够打开supreme的网站(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");
                }
            }
    }
}

谢谢!

0 个答案:

没有答案