fysh711426 / UndetectedChromeDriver

GNU General Public License v3.0
166 stars 60 forks source link

access to chromedriver and userDataDir is denied #48

Open alphaumi3 opened 1 year ago

alphaumi3 commented 1 year ago

I got this error: image

If I change back to using Ordinary Selenium, then my code can works normally. Bellow is my code using SeleniumUndetectedChromedriver: '// Thiết lập đường dẫn tới chromedriver string chromeDriverPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SavedData");

            // Tạo một thực thể của ChromeOptions
            var options = new ChromeOptions();

            // Thêm các option vào đối tượng ChromeOptions
            options.AddArgument("--disable-blink-features=AutomationControlled");
            options.AddArgument("--start-maximized");
            options.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36");

            // Tạo một thực thể của ChromeDriver và truyền ChromeOptions vào
            driver = UndetectedChromeDriver.Create(
                options: options,
                driverExecutablePath: chromeDriverPath,
                hideCommandPromptWindow: true,
                userDataDir: chromeDriverPath);

            // Truy cập website kiểm tra hồ sơ khách hàng Mirae Asset
            driver.Navigate().GoToUrl("https://myexampleurl.com/login");

            // Kiểm tra nếu đã có cookies trước đó cho tài khoản đang chọn thì sử dụng để đăng nhập
            string cookiesFilePath = Path.Combine(chromeDriverPath, "Cookies", $"{selectedUsername}_cookies.txt");
            if (File.Exists(cookiesFilePath))
            {
                string cookieContent = File.ReadAllText(cookiesFilePath);
                var cookies = JsonConvert.DeserializeObject<List<Cookie>>(cookieContent);

                // Thêm cookie vào browser
                foreach (var cookie in cookies)
                {
                    driver.Manage().Cookies.AddCookie(cookie);
                }
            }`

Please help!

fysh711426 commented 1 year ago

What about separating driverExecutablePath from userDataDir?

alphaumi3 commented 1 year ago

I try, but it didn't work!

fysh711426 commented 1 year ago

Check folder permissions.

alphaumi3 commented 1 year ago

When I use explicitly path, then chromedriver can launch, but this way is not convenient for user.

fysh711426 commented 1 year ago

I understand, driverExecutablePath needs to set the chromedriver exe path, but you set the folder path.

but this way is not convenient for user.

You can also use automatic download.

var driverExecutablePath = await new ChromeDriverInstaller().Auto();