zoukankan      html  css  js  c++  java
  • C#:在WebBrowser控件中阻止alert[警告框],confirm[确认对话框]对话框」及解析网页框架集\触发按钮事件

    代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using mshtml;

    namespace GoodClient
    {
        
    public partial class FrmMain : Form
        {
            
    public FrmMain()
            {
                InitializeComponent();


            }

            
    private void button1_Click(object sender, EventArgs e)
            {
                
    if (this.comboBox1.Text != "")
                {
                    
    try
                    {
                        webBrowser1.Navigate(
    this.comboBox1.Text);
                    }
                    
    catch { }
                }
            }

            
    private void button2_Click(object sender, EventArgs e)
            {
                
    if (this.comboBox2.Text != "")
                {
                    
    try
                    {
                        webBrowser2.Navigate(
    this.comboBox2.Text);
                    }
                    
    catch { }
                }
            }

            
    /*在WinForm中设置要使用组合键的窗体的KeyPreview(向窗体注册键盘事件)属性为True;
              然后使用窗体的KeyDown事件(在首次按下某个键时发生).
            
    */
            
    private void Form1_KeyDown(object sender, KeyEventArgs e)
            {

                
    if (e.KeyCode == Keys.F5)
                {
                    button1_Click(sender,  e);
                }
                
    if (e.KeyCode == Keys.F6)
                {
                    button2_Click(sender, e);
                }
            }

            
    private void Form1_Load(object sender, EventArgs e)
            {
                
    this.comboBox1.Text = http://aaaa.com/;
            }

            
    private void button3_Click(object sender, EventArgs e)
            {

                
    if (webBrowser1.Document.Window.Frames == null)
                    
    return;

                HtmlDocument htmlDoc 
    = webBrowser1.Document.DomDocument as HtmlDocument;
                
    int row = 0;

                
    //订单页   
                this.textBox1.Text = webBrowser1.Document.Window.Frames[0].Frames[1].Document.Body.InnerHtml;

                
    // row = webBrowser1.Document.Window.Frames.Count;
                
    //for (int i = 0; i < row; i++)
                
    //{
                
    //    MessageBox.Show(webBrowser1.Document.Window.Frames[i].Document.Body.InnerHtml, row.ToString() + "A");

                
    //}

                
    ////框架集
                // row = webBrowser1.Document.Window.Frames[0].Frames.Count;
                
    //for (int i = 0; i < row; i++)
                
    //{
                
    //    //MessageBox.Show(webBrowser1.Document.Window.Frames[0].Frames[i].Document.Body.InnerHtml , i.ToString() + "B");
                
    //    //MessageBox.Show(webBrowser1.Document.Window.Frames[0].Frames[i].Name, i.ToString() + "Name");
                
    //}

                
    ////左框架列表页
                row = webBrowser1.Document.Window.Frames[0].Frames[2].Frames.Count;
                
    for (int i = 0; i < row; i++)
                {
                   
    // MessageBox.Show(webBrowser1.Document.Window.Frames[0].Frames[2].Frames[i].Document.Body.InnerHtml, i.ToString() + "T");
                    this.textBox2.Text = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[i].Document.Body.InnerHtml;
                }

                
    //htmlDoc = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[1].Document;
                
    //System.Windows.Forms.HtmlElementCollection myLiks = htmlDoc.Links;   
                
    //foreach (HtmlElement link in myLiks)
                
    //{     
                
    //    this.textBox1.Text += link.GetAttribute("href") + "\r\n ";               
                
    //}
                
            }

            
    private void button4_Click(object sender, EventArgs e)
            {
                
                HtmlDocument htmlDoc 
    = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[1].Document;
                System.Windows.Forms.HtmlElementCollection myLiks 
    = htmlDoc.Links;
                
    string tmp_ads_url = "&type=C&gnum=30241&odd_f_type=H";
                
    foreach (HtmlElement link in myLiks)
                {                
                    
    if (link.GetAttribute("href").Contains(tmp_ads_url))
                    {
                        link.InvokeMember(
    "click");
                        
                        
    break;
                    }
                }

            }

            
    private void button6_Click(object sender, EventArgs e)
            {

                HtmlDocument htmlDoc 
    = webBrowser1.Document.Window.Frames[0].Frames[1].Document;
                
    foreach (HtmlElement h in htmlDoc.All)
                {
                    
    if (h.GetAttribute("type").ToString() == "button")
                    {

                        
    if (h.GetAttribute("value").ToString() == "确定")
                        {
                            h.InvokeMember(
    "onclick");
                            
    break;
                        }
                    }
                }

            }

            
    private void button5_Click(object sender, EventArgs e)
            {
                HTMLDocument doc 
    = webBrowser1.Document.DomDocument as HTMLDocument;
                HtmlWindow window 
    = webBrowser1.Document.Window.Frames[0].Frames[1];
                doc 
    = window.Document.DomDocument as HTMLDocument;

                HTMLInputElementClass input 
    = doc.all.item("gold"0as HTMLInputElementClass;
                input.value 
    = "1";

              


            }
            //阻止alert[警告框],confirm[确认对话框]对话框 
            
    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
                
    //IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
                
    //vDocument.parentWindow.execScript(
                
    //    "function alert(str){if(str=='未输入下注金额')confirm(str);}", "javaScript");


     

                HtmlDocument doc 
    = webBrowser1.Document.Window.Frames[0].Frames[1].Document;
                
    //框架结构中,必须正确的指定目标框架 
                IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow; 


                
    string s = @"function confirm() {";
                s 
    += @"return true;";
                s 
    += @"}";

                s 
    += @"function alert() {}";

                win.execScript(s, 
    "javascript"); 


            }





            

            //***********************************************************************************************
            //HtmlWindow window = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[1];
            //htmlDoc = window.Document.DomDocument as HtmlDocument;
            ////this.textBox2.Text = htmlDoc.Body.InnerHtml;
            //按钮点击事件
            //HtmlElement btnElement = htmlDoc.All["btnClose"];
            //if (btnElement != null)
            //{    
            //     btnElement.Click += new HtmlElementEventHandler(btnElement_Click);
            //}

            //doc.GetElementById("SearchUnitBtn").InvokeMember("click");

            //*******************************************************************************************
            ////存取网页的DOM元素:
            ////HtmlElement euser = webBrowser1.Document.All["txtUserName"];
            ////HtmlElement bSubmit = webBrowser1.Document.All["Button1"];

            ////euser.SetAttribute("value","想设置的值"); //设置DOM元素value属性
            ////bSubmit.InvokeMember("click"); //激发该元素的click事件


        }
    }
  • 相关阅读:
    【笔记】C++自学笔记系列02:类的组合与类的友元
    【windows程序设计】系列02:显示屏幕分辨率
    【笔记】C++自学笔记系列01:基础知识
    【windows程序设计】系列03:窗口与消息
    【windows程序设计】系列04:文本输出
    【Boost】系列05:数值与字符串转换
    【windows程序设计】系列01
    【Boost】系列04:实用技巧之禁止类拷贝构造和赋值
    【gdb】基本操作
    性能测试基本流程介绍(《软件性能测试过程详解与安全剖析》)
  • 原文地址:https://www.cnblogs.com/Fooo/p/1822218.html
Copyright © 2011-2022 走看看