How to avoid Access Denied when using selenium browser automation

Viewed 4188

I will try to describe the problem with a sample code. Here is a code in C# that opens an instance of a Chrome browser and navigates to nseindia.com:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace nseindia_selenium
{
    class Program
    {
        static void Main( string [ ] args )
        {
            ChromeOptions options = new ChromeOptions ();
            options.BinaryLocation = "C:\\Users\\Subhasis\\AppData\\Local\\Chromium\\Application\\chrome.exe";
            //options.AddAdditionalCapability ( "w3c" , true );
            options.AddArgument ( "no-sandbox" );
            options.AddArgument ( "start-maximized" );
            options.AddArgument ( "disable-gpu");
            options.AddArgument ( "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36" );
            options.AddExcludedArgument ( "enable-automation" );
            options.AddAdditionalCapability ( "useAutomationExtension" , false );
            //options.AddAdditionalCapability ( "chrome.page.customHeaders.referrer" , "https://www.nseindia.com" );
            ChromeDriver chrome1 = new ChromeDriver (options);
            chrome1.Navigate ().GoToUrl ( "https://www.nseindia.com/" );
        }
    }
}

Things seem to be working this far (not really, all data fields appear empty). But at this stage,if I take manual control of the browser window and try to browse to any other part of nseindia.com, I get an error: enter image description here

At this point, even if I try to go back to the homepage of the website, it does not let me do it.

Past answers of this same question recommended manually setting the referrer. But when I do,

options.AddAdditionalCapability ( "chrome.page.customHeaders.referrer" , "https://www.nseindia.com" );

it is giving me "Invalid argument" error. Also, if it is a fault of referrer, that does not explain why manualy clicking a link does not work.

The past answers had also recommended using

options.AddAdditionalCapability ( "useAutomationExtension" , false );

But that does not work anymore because I get this message:

[1601022727.512][WARNING]: Deprecated chrome option is ignored: useAutomationExtension
[1601022727.512][WARNING]: Deprecated chrome option is ignored: useAutomationExtension

In the past this used to happen because of wrong w3c mode, but I can not switch w3c mode anymore either. when I put

options.AddAdditionalCapability ( "w3c" , true );

I get an error saying:

System.ArgumentException: 'There is already an option for the w3c capability. Please use the  instead.
Parameter name: capabilityName'

So what do I do?

0 Answers
Related