Alex
Alex

Reputation: 44395

How to use credentials provider in selenium with python?

With python I am trying to use the credentials provider to provide credentials when connecting to a website for automated GUI testing with selenium. I found the following page which explains how to do this, possibly for JAVA:

@Override
protected WebClient newWebClient() {
    WebClient client = super.newWebClient();
    DefaultCredentialsProvider  provider = new DefaultCredentialsProvider();
    provider.addCredentials("username","password");
    client.setCredentialsProvider(provider);
    return client;
    }

I am trying to pythonize is, but I run into problems, and I do not find the appropriate class name fr the DefaultCredentialsProvider:

from selenium import webdriver as original_webdriver

class webdriver(original_webdriver):
    def newWebClient(self):
        client = super().newWebClient()
        provider = DefaultCredentialsProvider()
        provider.addCredentials("username","password")
        client.setCredentialsProvider(provider)
        return client

The error when running this script is:

  File "C:/Users/adi0341/PycharmProjects/SeleniumTest/tester.py", line 12, in <module>
    class webdriver(original_webdriver):
TypeError: module.__init__() takes at most 2 arguments (3 given)

How to fix it? Or how to do something similar as explained in that link? Maybe there is an altogether different approach to provide authentication in order to open a web page for selenium automated GUI-testing?

P.S: The authentication will be an essential part of the testing itself. Logging in as different users and check access rights...

Upvotes: 1

Views: 2663

Answers (3)

Abbas
Abbas

Reputation: 4069

Step 1

For this requirement use keyring

import keyring
keyring.set_password("https://my.sharepoint.come", "username", "password")

After this the credentials will be stored under credentials manager for automatic login, you can run this control /name Microsoft.CredentialManager command in a command prompt to get it:

enter image description here

Newly added credentials will appear under "Generic Credentials"

enter image description here

Further more even before you write the code you can test this manually.

Step 2

Once you are through with this, you need to set the preference of Firefox to hold your url under network.automatic-ntlm-auth.trusted-uris:

from selenium import webdriver
url = 'http://my.sharepoint.com'
fp = webdriver.FirefoxProfile()
fp.set_preference('network.automatic-ntlm-auth.trusted-uris',url)
driver = webdriver.Firefox(firefox_profile=fp)
driver.get(url)

Upvotes: 1

mootmoot
mootmoot

Reputation: 13176

DefaultCredentialsProvider ship with Java HtmlUnit, not webdriver. So you cannot find it under webdriver (whether in Java webdriver nor Python webdriver nor etc webdriver)

check this reference : How do I handle authentication with the HtmlUnitDriver using Selenium WebDriver?

Can you check whether there is similar python counter part for DefaultCredentialsProvider. Otherwise, the answer is : there is no DefaultCredentialsProvider for you in python.

Perhaps you should look for other authentication solution, such as this: Authentication with selenium (Python)

Upvotes: 0

user2678074
user2678074

Reputation: 825

Can you point which line is 12? I am unsure about your line with super, as in python inheritance of constructors looks like in this example.

Upvotes: 0

Related Questions