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.
Related
I am trying to use Python to automate a number of work processes that primarily take place in an online content management system, and to access the CMS, I need to be logged into my work profile on Chrome. While I could sign in once Chromedriver is open, I would have to approve the login on my authenticator each time, somewhat defeating the purpose of automation.
I am using the following code for opening Chromedriver and attempting to load my regular Chrome profile. However, while the rest of the code works as intended, it does not load my profile and I am unsure why.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--start-maximized")
options.add_argument("--user-data-dir=C:\\Users\\Saul\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome("C:\\Users\\Saul\\Downloads\\chromedriver_win32\\chromedriver.exe", options=options)
driver.get("https://myaccount.google.com/")
while(True):
pass
I do get the following error while running the code, but I don't believe this is affecting my issue.
C:\Users\Saul\PycharmProjects\OnlineAutomation\main.py:12: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome("C:\\Users\\Saul\\Downloads\\chromedriver_win32\\chromedriver.exe", options=options)
Your question is not related to the error you mentioned.
For the error - 'DeprecationWarning: executable_path has been deprecated, please pass in a Service object', you have to use Service:
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service("C:\\Users\\Saul\\Downloads\\chromedriver_win32\\chromedriver.exe"), options=options)
For the Chrome profile issue, you have to mention the profile path like below:
options.add_argument("--user-data-dir=C:\\Users\\Saul\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("--profile-directory=Default")
If your website has MFA enabled, you might be able to use SeleniumBase (a Python framework), which has methods for handling MFA Login:
📘📝 An example test with the BaseCase class. Runs with pytest or nosetests. (Learn more)
from seleniumbase import BaseCase
class TestMFALogin(BaseCase):
def test_mfa_login(self):
self.open("https://seleniumbase.io/realworld/login")
self.type("#username", "demo_user")
self.type("#password", "secret_pass")
self.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
self.assert_exact_text("Welcome!", "h1")
self.assert_element("img#image1")
self.click('a:contains("This Page")')
self.save_screenshot_to_logs()
📗📝 An example test with the sb pytest fixture. Runs with pytest.
def test_mfa_login(sb):
sb.open("https://seleniumbase.io/realworld/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
sb.assert_exact_text("Welcome!", "h1")
sb.assert_element("img#image1")
sb.click('a:contains("This Page")')
sb.save_screenshot_to_logs()
📙📝 An example test with the SB Context Manager. Runs with pure python.
from seleniumbase import SB
with SB() as sb: # By default, browser="chrome" if not set.
sb.open("https://seleniumbase.github.io/realworld/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
sb.assert_text("Welcome!", "h1")
sb.highlight("img#image1") # A fancier assert_element() call
sb.click('a:contains("This Page")') # Use :contains() on any tag
sb.click_link("Sign out") # Link must be "a" tag. Not "button".
sb.assert_element('a:contains("Sign in")')
sb.assert_exact_text("You have been signed out!", "#top_message")
There's a SeleniumBase pytest command-line option, --user-data-dir=DIR, which you can add to set a user data directory to use. Eg:
pytest --user-data-dir=DIR
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.
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.
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.
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)