How to authenticate multiple hosts using selenium webdriver? - python

I am trying to run tests against a website that requires four different sets of basic auth. There is all sorts of content embedded in the site requiring separate auth. For example:
http://store.domain.com
http://content.domain.com
https://content.domain.com
https://store.domain.com
Another way to say this is that when I hit one URL, I get four different sets of auth pop ups.
I'm not sure how to tackle this via Selenium. I currently have a working test written in Python / Selenium Web Driver, but when I run it I am forced to manually keep entering in creds each time, which is not a viable solution as this is going to be run from a TC build on a remote server.
Since it seems like Selenium isn't very flexible with auth stuff, I thought maybe as I workaround I could just hit each of the URLs requesting auth, pass them creds (this would happen during setup), and then start the actual tests. I need help with this for multiple reasons.
class newSmokeTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://domain.com"
self.verificationErrors = []
self.accept_next_alert = True
driver = self.driver
# sending auth credentials early to avoid prompts while running test suite
driver.get("http://username:pass#store.domain.com")
wait = WebDriverWait(driver, 10)
driver.get("https://username:pass#store.domain.com")
wait = WebDriverWait(driver, 10)
driver.get("http://username:pass#content.domain.com")
wait = WebDriverWait(driver, 10)
driver.get("https://username:pass#content.domain.com")
def test1(self):
#do stuff
def test2(self):
#do stuff
One, I'm not even sure if that is a viable solution, would manually passing creds to each URL in the setup of my unittest create a session in Firefox, how do I find that out?
Two, when I run the test, I actually don't visually see the browser hitting those URL's. Is putting those steps in the setUp for some reason preventing those steps from running?
I'm kind of at a dead end with this, would appreciate any help. Using a Firefox profile is not the answer I'm looking for as this will be ran on a remote server by another department and I'm not even sure that they would allow the installation of a Firefox profile on there. Hoping I can do this in a more Pythonic way.

Related

Reuse webdriver session to prevent reload page?

Intro: I am trying to scrape a page without reloading it and I need many user use the same page again without launching new webdriver sessions.
I was following this Re-using existing browser session in selenium
But when I used the following code I am not able to create new sessions (driver = webdriver.Chrome(chromedriverPath)) anymore because the seision_id always the same
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
# Save the original function, so we can revert our patch
org_command_execute = RemoteWebDriver.execute
def new_command_execute(self, command, params=None):
if command == "newSession":
# Mock the response
return {'success': 0, 'value': None, 'sessionId': session_id}
else:
return org_command_execute(self, command, params)
# Patch the function before creating the driver object
RemoteWebDriver.execute = new_command_execute
Goals in my website I have too many users, so I want to prevent using too many requests as much as possible. Hence, I am trying to lunch 3 or 4 seasons then I use one of them.
Now, I have too many other things I may need to explain.
For example to prevent the possibility of two users clicking the same button the same time I created a data_base where it has the driver session_id and a field called is_used and a toggle it False/True.
Also, when I user need data from the website I filter the database that I have then like this DriverSesions.objects.filter(is_used=False)
question
How can I use this article without losing the ability to launch new session?
Are their alternatives?

Automation on the site using seleniumrequests

I am trying to automate some processes on the site. At first I tried to use queries, but a captcha came in response. Now I'm using selenium queries, and here's the problem: when I log in using selenium tools only, everything works fine, but I can't add coupons on the site and confirm them.
from seleniumrequests import Firefox
driver = Firefox()
user = '000000'
password = '000000'
driver_1x.get("https://1xstavka.ru/")
driver.find_element_by_id('curLoginForm').click()
driver.find_element_by_id('auth_id_email').send_keys(user)
driver.find_element_by_id('auth-form-password').send_keys(password)
driver.find_element_by_class_name('auth-button__text').click()
But if you use:
from seleniumrequests import Firefox
driver = Firefox()
driver.request('GET', 'https://1xstavka.ru')
The window opens for a second and immediately closes, a 200 response is received, but there are no cookies. It's the same with publishing requests, with which I'm trying to automate the process. After the request for publication, the response is 200, but nothing happens on the site.
driver.request('POST', 'https://1xstavka.ru/user/auth', json=json)
please tell me what is wrong or how you can solve this problem
I am unable to access the URL specified in the query; but for captcha/coupons, I created a loop with an interim stop function. This gives me the chance to input it manually and then continue the loop.

Failed to use selenium to automatically click the link in a website

I want to use selenium to automatically log in a website(https://www.cypress.com/) and download some materials.
I successfully open the website using selenium. But when I use selenium to click the "Log in" button. It shows this:
Access Denied
Here is my code:
from time import sleep
from selenium import webdriver
class Cypress():
def extractData(self):
browser = webdriver.Chrome(executable_path=r"C:chromedriver.exe")
browser.get("https://www.cypress.com/")
sleep(5)
element = browser.find_element_by_link_text("Log in")
sleep(1)
element.click()
pass
if __name__ == "__main__":
a = Cypress()
a.extractData()
pass
Can anyone give me some idea?
The website is protected using Akamai CDN, services, or whatever is loaded there.
I took a quick glance and it seems like the Akamai service worker is up, but I don't see any sensor data protection, selenium is simply detected as webdriver (and plenty other things) and flagged, try to login using requests, or ask the website owner to give you API access for your project.
Akamai cookies are up, so surely the protection is too, the 301 you got is the bot protection stopping you from automating something on a protected endpoint.

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...
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:
Newly added credentials will appear under "Generic Credentials"
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)
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.
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)

selenium Firefox webdriver python: setting user authentication in setUp()

I am trying to write a selenium testing case using Python bindings. I am very new to this, so I am really at loss.
My problem: The test is to check basic functionality for wordpress blogs, so logging in is required. I am using unittest, and Im trying to set up user authentication in setUp(), so every time before new test case runs, each cases is logged in as admin.
I have done some researches and tried things without luck. Here is what I have so far.
def setUp(self):
prof_dir = "C:/Python27/python_tests/ff_profile"
firefox_profile = webdriver.FirefoxProfile(prof_dir)
firefox_profile.native_events_enables = True
self.driver = webdriver.Firefox(firefox_profile)
self.base_url = "https://blogaddress.com"
self.verificationErrors = []
self.driver.get(self.base_url + "/wp-login.php")
I have saved credentials in profile, so I would expect when webdriver reaches the login page is already filled with credentials. However, it does not do that, and I cannot find a way to set up credentials in setUp(). (I tried quick and dirty way of loggin a user in by sending keys directly but this is just not good)
Is there any good ideas on setting credentials in setUp()?
Thanks for your help.

Categories

Resources