简体   繁体   English

使用 Puppeteer-sharp 启动 Tor 浏览器

[英]Launch Tor Browser with Puppeteer-sharp

I'm trying to launch Tor browser via puppeteer-sharp .我正在尝试通过puppeteer-sharp启动Tor浏览器。 I am using .net core 3.1 console application and latest version of puppeteer-sharp .我正在使用.net core 3.1控制台应用程序和最新版本的 puppeteer puppeteer-sharp So far the given the executable path console application launches the Tor Browser with an exception.到目前为止,给定的可执行路径控制台应用程序启动 Tor 浏览器时出现异常。

using PuppeteerSharp;
using System.Threading;
using System.Threading.Tasks;

 
namespace puppeteer_tor
{
    internal class Program
    {
        static async Task Main(string[] args)
        {

            string enableAutomation = "--enable-automation";
            string noSandBox = "--no-sandbox";
            string disableSetUidSandBox = "--disable-setuid-sandbox";
            string[] argumentsWithoutExtension = new string[] { "C:\\Users\\selaka.nanayakkara\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\profile.default", "--proxy-server=socks5://127.0.0.1:9050", "--disable-gpu", "--disable-dev-shm-usage", enableAutomation, disableSetUidSandBox, noSandBox };

            var options = new LaunchOptions
            {
                Headless = false,
                ExecutablePath = @"C:\Users\selaka.nanayakkara\Desktop\Tor Browser\Browser\firefox.exe",
                Args = argumentsWithoutExtension
            };

            using (var browser = await Puppeteer.LaunchAsync(options))
            {
                Thread.Sleep(5000);
                var page = await browser.NewPageAsync();
                await page.GoToAsync("https://check.torproject.org/");
                var element = await page.WaitForSelectorAsync("h1");
                var text = element.ToString();

            }
        }

    }

}

The browser launches with an issue and gives me the exception of:浏览器启动时出现问题,并给了我以下例外:

Failed to launch browser!无法启动浏览器!

With the below screen of the Tor browser:使用 Tor 浏览器的以下屏幕:

在此处输入图像描述

Your help is much appreciated in the above issue.非常感谢您在上述问题中的帮助。 Thanks in advance.提前致谢。

Please find the attach code base here .请在此处找到附加代码库。

Set the Headless to true nad try将 Headless 设置为 true nad try

var options = new LaunchOptions
{
    Headless = true,
    ExecutablePath = @"C:\Program Files\Mozilla Firefox\firefox.exe",
    Args = argumentsWithoutExtension
};

After many pitfalls I was able to find the puppeteer-sharp to work along with Tor Browser.在经历了许多陷阱之后,我终于找到了可以与 Tor 浏览器一起使用的 puppeteer-sharp。 For anyone who is interested please find the below code attached here with:对于任何有兴趣的人,请在此处找到以下代码:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using PuppeteerSharp;
using System;
using System.Threading;
using System.Threading.Tasks;


namespace puppeteer_tor
{
    internal class Program
    {
        static async Task Main(string[] args)
        {


            // Initiating Browser configuration
            Console.WriteLine("Intiating Tor Browser");
 
             Browser browser = (Browser)await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = false,
                ExecutablePath = @"C:\Users\selaka.nanayakkara\Desktop\Tor Browser\Browser\firefox.exe",
                Product = Product.Firefox,
                UserDataDir = @"C:\Users\selaka.nanayakkara\Desktop\Tor Browser\Browser\TorBrowser\Data\profile.default",
                DefaultViewport = null,
                IgnoreHTTPSErrors = true,
                Args = new[] { "-wait-for-browser" }
            });

            // Enabling prxoy connectivilty
            Console.WriteLine("Intiating Tor proxy");
            var page = await browser.PagesAsync();
            Page page1 =(Page)page[0];
            await page1.ClickAsync("#connectButton");

            // Loading geoblocked url.
            Console.WriteLine("Navigating to the URL");
            Page page3 =(Page)await browser.NewPageAsync();
            page3.DefaultNavigationTimeout = 0;
            await page3.GoToAsync("http://nebraskalegislature.gov/laws/browse-chapters.php?chapter=20");

            // Fetching content from the page.
            Console.WriteLine("Fetching content in the URL.");
            var content = await page3.GetContentAsync();
            
            Console.WriteLine("Content fetching completed! ");

            // Closing Browser
            Console.WriteLine("Closing browser.");
            await browser.CloseAsync();

         }
    }

 }

Sample git repository: https://github.com/SelakaKithmal/puppeteer-tor示例 git 存储库: https://github.com/SelakaKithmal/puppeteer-tor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM