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);

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐