the cursor is in the active window from the very beginning, but the input into the field does not occur. There is no way to use find_element for the input, I try switching fields through the TAB button. the site itself and its form open normally
url I'm working with: https://the-internet.herokuapp.com/basic_auth
full code
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class TestLogin:
driver = ''
def setup_method(self):
self.driver = webdriver.Chrome(executable_path='D:\WORK\PycharmProjects\at\chromedriver.exe')
self.driver.implicitly_wait(2)
self.driver.get('https://the-internet.herokuapp.com/basic_auth')
def test_login(self):
action = ActionChains(self.driver)
# user name
action.send_keys(Keys, "admin")
# move to next field
action.send_keys(Keys.TAB)
# password
action.send_keys("admin")
# try to login
action.send_keys(Keys.ENTER)
action.perform()
# check login
self.driver.find_element(By.XPATH, "//div[contains(text(), 'Congratulations!')]")
def teardown_method(self):
self.driver.quit()
CodePudding user response:
This is one way of handling basic authentication in selenium:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
user = 'admin'
passw = 'admin'
browser.get(f"https://{user}:{passw}@the-internet.herokuapp.com/basic_auth")
test_element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//p[contains(text(), 'Congratulations!')]")))
print(test_element.text)
This will print in terminal:
Congratulations! You must have the proper credentials.
You can include the logic above in your unit test classes (or rethink your logic and get rid of unnecessary OOPing everything) - your call.