SeleniumBasic icon indicating copy to clipboard operation
SeleniumBasic copied to clipboard

Loading Chrome with Javascript disabled

Open quentinharris opened this issue 6 years ago • 2 comments

Is there a way, in VBA, to load a page, using Chrome, without allowing Javascript scripts to run? I want ideally to find a way to load without scripts running and then sequentially execute scripts to view the changes on the page. Is this even possible?

For starters I was looking at this StackOverflow question. This showed Java implementation of:

FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("javascript.enabled", false); WebDriver driver = new FirefoxDriver(profile);

I cannot see an object called ChromeProfile in selenium basic. I tried shorthand of:

`

Option Explicit
Public Sub ViewInfo()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://finance.yahoo.com/most-active?offset=0&count=100"
With d
    .SetPreference "javascript.enabled", False
    .Start "Chrome"
    .get URL
    .FindElementByTag("form").submit
    Stop
    .Quit
End With
End Sub

`

This doesn't appear to have stopped JS scripts from running on the page. I expected the canvas tag elements, in the last column of the stocks table, to not have images if this were the case.

So, I wondered if, as per this question, I have to create a Chrome Profile with JS disabled (assuming that is possible?) and load that?

Any help gratefully received.

quentinharris avatar Oct 15 '18 11:10 quentinharris

What if you try Chrome Binary with profile already setup with browser preferences to block js

sanjoyforever avatar Oct 16 '18 05:10 sanjoyforever

What if you try Chrome Binary with profile already setup with browser preferences to block js

@sanjoyforever Thanks for responding. What is Chrome binary please?

To try and set up a profile with JS disabled I simply went to the menu in Chrome and added a new user (user2) and in advanced options blocked Javascript. I then used the following script to test:

Option Explicit
Public Sub GetInfo()
Dim d As WebDriver
Const URL = "https://login.microsoftonline.com/"
Const NO_JS_PROFILE As String = "C:\Users\User\AppData\Local\Google\Chrome\User Data\Profile 1"
Const JS_PROFILE As String = "C:\Users\User\AppData\Local\Google\Chrome\User Data\Default"
Set d = New ChromeDriver
With d
    .SetProfile NO_JS_PROFILE, True
    .Start "Chrome"
    .get URL
     Stop
    .Quit
End With
End Sub

If I manually switch to NO_JS_PROFILE in Chrome and then put URL into the navigation bar everything works as expected; I get a screen saying Javascript is required for Login.

However, if I run the above script with NO_JS_PROFILE as profile I do not get the expected result. Javascript still runs on the page and I don't see the associated profile name at the top right either. Am I doing something wrong please? I retrieved both paths by via chrome://version/.

quentinharris avatar Oct 16 '18 06:10 quentinharris