C# Chrome浏览器网页自动化操作

NuGet 包管理器 安装 WebDriver

 自动登录QQ邮箱

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
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;

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

        ChromeDriver webDriver;
        private void Form1_Load(object sender, EventArgs e)
        {
            ChromeOptions options = new ChromeOptions();
            webDriver =   new ChromeDriver(@"C:\Program Files\Google\Chrome\Application\", options);//Chrome目录
            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));//设置读取超时时间
            try
            {
                wait.Until(contion =>
            {
                try
                {
                    webDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);//加载页面超时
                    webDriver.Navigate().GoToUrl("https://mail.qq.com");

                    return true;
                }
                catch { return false; }
            }
            );
            }catch { }

            try
            {
                IWebElement controlPanelFrame = null;
                IList<IWebElement> frames = webDriver.FindElements(By.TagName("iframe"));
                foreach (var frame in frames)
                {
                    if (frame.GetAttribute("id") == "login_frame")
                    {
                        controlPanelFrame = frame;
                        break;
                    }
                }
                if (controlPanelFrame != null)
                {
                    //var a = webDriver.FindElement(By.XPath("//iframe[starts-with(@id, 'mainFrame')]"));
                    webDriver.SwitchTo().Frame(controlPanelFrame);//选择Frame
                }

                webDriver.FindElement(By.XPath("//input[contains(@id, 'u') and contains(@name, 'u')]")).SendKeys("qq号码@qq.com");
                webDriver.FindElement(By.XPath("//input[contains(@id, 'p') and contains(@name, 'p')]")).SendKeys("密码");
                string logintxt = webDriver.FindElement(By.XPath("//label[contains(@id, 'uin_tips')]")).GetAttribute("innerHTML");
                string loginbbbstr = webDriver.FindElement(By.XPath("//a[contains(@class, 'login_button')]")).GetAttribute("href");

                if (loginbbbstr == "javascript:void(0);" && logintxt== "支持QQ号/邮箱/手机号登录")
                {
                    webDriver.FindElement(By.XPath("//a[contains(@href, 'javascript:void(0);') and contains(@class, 'login_button')]")).Click();
                }

                webDriver.SwitchTo().DefaultContent();//跳出Frame
            }
            catch
            {

            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                webDriver.Quit();
            }
            catch
            { }
        }
    }
}

CNPM Binaries Mirrorhttps://registry.npmmirror.com/binary.html?path=chromedriver/99.0.4844.51/

var option = new ChromeOptions();  
//是否隐藏浏览器,如果注释掉的话,那么会在启动的时候打开谷歌浏览器,在页面上可以看到填单的操作,如果取消的话,就只有cmd窗口,谷歌浏览器隐匿运行        
option.AddArgument("headless");
options.AddArgument("--window-position=-32000,-32000");
option.AddArgument("window-size=1920,1080");

这个是题外话,需要的可以设置下,就是让谷歌浏览器不弹出来,隐匿运行

//设置cmd窗口不弹出来
ChromeDriverService service = ChromeDriverService.CreateDefaultService(System.AppDomain.CurrentDomain.BaseDirectory.ToString());
// ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"C:\Program Files\Google\Chrome\Application\");
service.HideCommandPromptWindow = true;
webDriver = new ChromeDriver(service, option);

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值