Set preference method giving compile error as "Argument not optional" in SeleniumVBA

Viewed 427

I am trying to use the SetPreference() method in Selenium with VBA to login to website using user details.

Below is the code.

Sub setttt()

    Dim driver As New ChromeDriver    
    Set driver = New ChromeDriver
    driver.Get ("https:www.gmail.com/")    
    driver.AddArgument ("--no-sandbox")      
    driver.SetPreference ("user-data-dir=C:/Users/sagar/AppData/Local/Google/Chrome/User Data/Default")
    driver.Wait (500)

End Sub

I am getting a compile error

Argument not optional

Please help if I am missing anything here.

2 Answers

You have a couple of other issues with your code. First, you need to set the profile of the user directory prior to starting Chrome. So, this would need to be set before using the .Get() method.

Also, if you are using Dim .. As New in your declaration, you do not need to Set .. = New later on.

Finally, instead of using .SetPreference() to grab your user profile, just use .SetProfile() with persistent:=True.

And I should also mention that you do not need to include \Default to the profile, this is included automatically; and a personal preference would be for me to use %USERPROFILE% instead of hard coding the username in the filepath.

Dim driver As New ChromeDriver

driver.SetProfile "%USERPROFILE%\AppData\Local\Google\Chrome\User Data", True
driver.AddArgument "--no-sandbox"
driver.Get "https://www.gmail.com/"

For your specific issue, that error code implies that you are missing a required argument. .SetPreference requires two arguments:

  1. Key
  2. Value

But you only provided one.

Try remove the parentheses

driver.AddArgument "--no-sandbox"

if still not solved try with

driver.Get "https:www.gmail.com/"
driver.AddArgument "--no-sandbox"
driver.SetPreference "user-data-dir", "C:/Users/sagar/AppData/Local/Google/Chrome/User Data/Default"
Related