分享

puppeteerSharp 浏览器初始化

 丰收书屋 2023-09-18
  1. /// <summary>
  2. /// 页面宽
  3. /// </summary>
  4. public int ViewportWidth { set; get; } = 1920;
  5. /// <summary>
  6. /// 页面高
  7. /// </summary>
  8. public int ViewportHeight { set; get; } = 1080;
  9. /// <summary>
  10. /// 获取参数
  11. /// </summary>
  12. /// <param name="ipModel">代理Ip</param>
  13. /// <returns></returns>
  14. public LaunchOptions GetLaunchOptions(IpModel ipModel = null)
  15. {
  16. //从网络上下载浏览器便捷式安装包download-Win64-536395.zip到你本地,里面解压后是一个Chromium浏览器,这里需要等待一些时间
  17. //下载完第二次进来就不会再去下载了
  18. if (string.IsNullOrEmpty(AppSetting.ExecutablePath))
  19. {
  20. throw new Exception("请先设置Chromium浏览器驱动文件路径");
  21. }
  22. if (!File.Exists(AppSetting.ExecutablePath))
  23. {
  24. throw new Exception("Chromium浏览器驱动文件不存在");
  25. }
  26. var options = new LaunchOptions
  27. {
  28. //是否无头浏览器
  29. Headless = false,
  30. Args = new[] {
  31. "--start-maximized",//最大窗口
  32. "--disable-features=site-per-process" // 加这个可以查看到iframe,用上面的就看不到iframe了
  33. },
  34. IgnoreHTTPSErrors = true,
  35. ExecutablePath = AppSetting.ExecutablePath
  36. };
  37. if (ipModel != null)
  38. {
  39. options.Args = new[]
  40. {
  41. $"--proxy-server=http://{ipModel?.Ip}:{ipModel?.Port}",//代理ip
  42. "--start-maximized",//最大窗口
  43. "--disable-features=site-per-process" // 加这个可以查看到iframe,用上面的就看不到iframe了
  44. };
  45. }
  46. return options;
  47. }

 

'--no-sandbox', // 不开启沙箱

'--disable-features=site-per-process', // 加这个可以查看到iframe,用上面的就看不到iframe了

`--window-size=${width},${height}`, // 设置窗口大小

'--disable-ios-password-suggestions', // 不弹出保存密码

 

timeout: 8000,

defaultViewport: {width: width, height: height}, // 设置内容显示具体大小

ignoreHTTPSErrors : true, // 忽略证书错误

devtools: true, // 开发者控制台

headless: false, // 以无头浏览器的形式打开浏览器,没有界面显示,在后台运行的

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多