public static async Task<string> LogInAsync()
{
try
{
string ResultCookies = "";
//获取用户名
string UserName = Environment.UserName; var currentDirectory = Path.Combine(@"C:\Users\", UserName, @"AppData\Local\Google\Chrome\Application\", "Chrome.exe");//string currentDirectory = Path.GetDirectoryName(@"C:\Users\TT\AppData\Local\Google\Chrome\Application"); //指定Chrome.exe在这目录才行 if (!File.Exists(currentDirectory))
{
currentDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
var downloadPath = Path.Combine(currentDirectory, "LocalChromium");
Console.WriteLine($"Attemping to set up puppeteer to use Chromium found under directory {downloadPath} ");
if (!Directory.Exists(downloadPath))
{
Console.WriteLine("Custom directory not found. Creating directory");
Directory.CreateDirectory(downloadPath); Console.WriteLine("Downloading Chromium"); var browserFetcherOptions = new BrowserFetcherOptions { Host = "https://npm.taobao.org/mirrors", Path = downloadPath };//设置淘宝镜像
var browserFetcher = new BrowserFetcher(browserFetcherOptions);
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision); var executablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision); if (string.IsNullOrEmpty(executablePath))
{
Console.WriteLine("Custom Chromium location is empty. Unable to start Chromium. Exiting.\n Press any key to continue");
Console.ReadLine();
return "Custom Chromium location is empty. Unable to start Chromium. Exiting.\n Press any key to continue";
}
Console.WriteLine($"Attemping to start Chromium using executable path: {executablePath}");
//Set Path
currentDirectory = Path.Combine(executablePath, "Chromium.exe");
}
else
{
//Set Path 这里没做下载失败的判断
currentDirectory = Path.Combine(downloadPath, "Chromium.exe");
} } var options = new LaunchOptions
{
Headless = false,//无头
ExecutablePath = currentDirectory,//本地路径
Args = new string[]
{
"--disable-infobars",//隐藏 自动化标题
},//添加Argument 和webdriver一样吧
DefaultViewport = new ViewPortOptions
{
Width = 500,
Height = 500,
IsMobile = true,
//DeviceScaleFactor = 2
},
//SlowMo=250, // slow down by 250ms
}; using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
// disable images to download
//await page.SetRequestInterceptionAsync(true);
//page.Request += (sender, e) =>
//{
// if (e.Request.ResourceType == ResourceType.Image)
// e.Request.AbortAsync();
// else
// e.Request.ContinueAsync();
//};
//设置手机模式
DeviceDescriptor deviceOptions = Puppeteer.Devices.GetValueOrDefault(DeviceDescriptorName.IPhone7);
await page.EmulateAsync(deviceOptions);
//await page.SetUserAgentAsync("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"); await page.GoToAsync("https://www.baidu.com/"); // 登录
Console.WriteLine("Start Login!");
await page.GetContentAsync();
//输入
//ElementHandle input = await page.WaitForSelectorAsync("#search_form_input_homepage");
//await input.TypeAsync("Lorem ipsum dolor sit amet.");
await page.TypeAsync("input[name=id]", "yourname");
await page.TypeAsync("input[name=pwd]", "yourpassword");
await Task.WhenAll(page.ClickAsync("#login"), page.WaitForNavigationAsync());
Console.WriteLine("Finish Login!"); //获取Cookies
//CookieParam[] cookies = await page.GetCookiesAsync(); Console.WriteLine(ResultCookies);
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
return ResultCookies;
}
catch (Exception ex)
{
return ex.Message;
}
}

官方代码

https://github.com/kblok/puppeteer-sharp

PuppeteerSharp体验之旅的更多相关文章

  1. 【Knockout.js 学习体验之旅】(3)模板绑定

    本文是[Knockout.js 学习体验之旅]系列文章的第3篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...

  2. 【Knockout.js 学习体验之旅】(2)花式捆绑

    本文是[Knockout.js 学习体验之旅]系列文章的第2篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...

  3. 【Knockout.js 学习体验之旅】(1)ko初体验

    前言 什么,你现在还在看knockout.js?这货都已经落后主流一千年了!赶紧去学Angular.React啊,再不赶紧的话,他们也要变out了哦.身旁的90后小伙伴,嘴里还塞着山东的狗不理大蒜包, ...

  4. Linux体验之旅(一)——制作U启,安装rhel-server-6.3

    U启制作: 双击UltraISO: 点击文件→打开: 选择rhel-server6.3 点击启动→选择写入硬盘映像 最后选择格式化优盘→写入→完毕 注意:启动盘制作完毕后一定记得将rhel-serve ...

  5. 用浏览器快速开启Docker的体验之旅

    互联网科技发展创造了很多奇迹,比如我今天要提到的 docker 技术就是其一.我很早就关注它(在2015年写过这方面的博客),那会儿还只是一个开源项目,现在已经是一个行业事实标准了,它推动了云原生的变 ...

  6. IBM Cognos 10.2 最新体验之旅

    IBM Cognos Data Manager 数据集市的构建利器 本文详细的介绍了 Cognos 最新版本 10.2 的数据集市构建器 Data Manager 的使用,对于希望系统了解 Cogno ...

  7. altera soc体验之旅 FPGA与ARM的窃窃私语

      喜大普奔,公司要评估用SOC做产品,我就自然而然的被安排了学习和评估的工作,于是,每天的工作就是开始研究soc了.其实,只要能静下心来学习,一切都还是能够弄出来的. 以前像个无头苍蝇一样到处乱撞, ...

  8. MySQL原生HA方案 – Fabric体验之旅

    http://www.csdn.net/article/2014-08-20/2821300

  9. HighChart 体验之旅 (后台传递JSON参数和数据的方法)

    转自:http://www.cnblogs.com/daviddai/archive/2013/04/12/Highchart.html 官网:http://www.highcharts.com/ 中 ...

随机推荐

  1. adb工具介绍与安装

    一天笑嘻嘻是一名测试人员,想了解Android的测试方法,于是,就找到了小测试. 笑嘻嘻:身为一名测试人员需要了解ADB的哪些内容? 小测试:了解原理和简单的命令使用就可以了. 笑嘻嘻:你有毒啊,都了 ...

  2. dual Oracle兼容

    CREATE OR REPLACE VIEW dual ASSELECT NULL::"unknown"WHERE 1 = 1;

  3. tp5.1中redis使用

    一.环境安装 1.下载redis,igbniary https://windows.php.net/downloads/pecl/releases/igbinary/ https://windows. ...

  4. python-day40(正式学习)

    目录 线程队列 1 2 3 线程定时器 进程池和线程池 线程队列 1 import queue q=queue.Queue() q.put('123') q.put('456') q.put('789 ...

  5. CentOS 7 关闭Selinux

    临时关闭: [root@localhost ~]# getenforceEnforcing [root@localhost ~]# setenforce 0[root@localhost ~]# ge ...

  6. 程序员必备的网站之Tutorialspoint

    程序员必备的网站之Tutorialspoint 给大家介绍一个非常好的网站Tutorialspointhttp://www.tutorialspoint.com/index.htm,也许好多人都已经用 ...

  7. IAP技术原理

    目录 IAP技术原理 更新记录 IAP与ISP的概念及原理 ISP简介 ISP原理 IAP简介 IAP原理 IAP优势 IAP的设计 1.程序启动流程 2.中断向量表的重定位 3.IAP跳转APP函数 ...

  8. Python(os和sys)理解

    Python(os和sys)理解 os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时 ...

  9. postgres中的函数

    1.编写一个只有入参,没有出参的函数: CREATE OR REPLACE FUNCTION add(a NUMERIC, b NUMERIC) RETURNS NUMERIC AS $$ SELEC ...

  10. List的Select 和Select().tolist()

    List<Person> delp = new List<Person> { ,Name=,Sign= }, ,Name= ,Sign=}, }; delp.Select(u ...