英文:
How to authenticate using Windows Authentication in Playwright
问题
需要自动化一个使用Windows身份验证的测试。
我知道打开的提示框不是HTML页面的一部分,但为什么我的代码不起作用:
login_page.click_iwa()
sleep(5)
self.page.keyboard.type('用户名')
sleep(5)
self.page.keyboard.press('Tab')
self.page.keyboard.type('密码')
英文:
I need to automate a test that uses Windows Authentication.
I know that the the prompt that opens up is not part of the HTML page, yet why my code is not working:
login_page.click_iwa()
sleep(5)
self.page.keyboard.type('UserName')
sleep(5)
self.page.keyboard.press('Tab')
self.page.keyboard.type('Password')
答案1
得分: 2
我通过使用虚拟键盘来解决了这个问题:
from pyautogui import press, typewrite, hotkey
def press_key(key):
press(key)
def type_text(text):
typewrite(text)
def special_keys(special, normal):
hotkey(special, normal)
然后,在我实现了虚拟键盘之后,我做了这个:
def login_iwa(self, user_name='User321', password='123456', need_to_fail=False):
pyautogui.FAILSAFE = False
self.click_iwa()
sleep(7)
type_text(user_name)
sleep(1)
press_key('Tab')
sleep(1)
type_text(password)
sleep(1)
press_key('Enter')
sleep(2)
if need_to_fail:
press_key('Tab')
sleep(1)
press_key('Tab')
sleep(1)
press_key('Tab')
sleep(1)
press_key('Enter')
sleep(1)
我将 pyautogui.FAILSAFE 设置为 False,因为有时弹出窗口会隐藏在屏幕后面或在第二个屏幕上打开。
英文:
I solved the issue by using a virtual keyboard:
from pyautogui import press, typewrite, hotkey
def press_key(key):
press(key)
def type_text(text):
typewrite(text)
def special_keys(special, normal):
hotkey(special, normal)
Then, after I implemented the virtual keyboard, I did this:
def login_iwa(self, user_name='User321', password='123456', need_to_fail=False):
pyautogui.FAILSAFE = False
self.click_iwa()
sleep(7)
type_text(user_name)
sleep(1)
press_key('Tab')
sleep(1)
type_text(password)
sleep(1)
press_key('Enter')
sleep(2)
if need_to_fail:
press_key('Tab')
sleep(1)
press_key('Tab')
sleep(1)
press_key('Tab')
sleep(1)
press_key('Enter')
sleep(1)
I used pyautogui.FAILSAFE = False because sometime the popup was hiding behind the screen or was openning up on the 2nd screen.
答案2
得分: -2
我建议使用更多基于协议的方法。我已经测试了以下方法来使用用户名和密码设置为admin进行身份验证,所有方法都成功。下面是每种方法的完整代码,以便您可以简单地复制粘贴进行测试:
- 在URL中传递凭据:
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto(url="http://admin:admin@the-internet.herokuapp.com/basic_auth")
context.close()
browser.close()
- 将凭据传递给
http_credentials上下文参数:
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(
http_credentials={
"username": "admin",
"password": "admin"
}
)
page = context.new_page()
page.goto(url="http://the-internet.herokuapp.com/basic_auth")
context.close()
browser.close()
- 使用
AuthorizationHTTP头(然而,还有其他身份验证类型):
import base64
from playwright.sync_api import sync_playwright
def generate_basic_authentication_header_value(username: str, password: str) -> str:
token = base64.b64encode("{}:{}".format(username, password).encode("utf-8")).decode("ascii")
return "Basic {}".format(token)
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(
extra_http_headers={
"Authorization": generate_basic_authentication_header_value("admin", "admin"),
}
)
page = context.new_page()
page.goto(url="http://the-internet.herokuapp.com/basic_auth")
context.close()
browser.close()
英文:
I recommend using more protocol-based approaches. I have tested the following methods to authenticate using the the-internet.herokuapp.com/basic_auth endpoint with the username and password set to admin, and all of them worked successfully. Below, you'll find the complete code for each method to make it easy to test them by simply copy-pasting:
-
Passing the credentials in the URL:
from playwright.sync_api import sync_playwright with sync_playwright() as playwright: browser = playwright.chromium.launch(headless=False) context = browser.new_context() page = context.new_page() page.goto(url="http://admin:admin@the-internet.herokuapp.com/basic_auth") context.close() browser.close() -
Passing the credentials to the
http_credentialscontext parameter:from playwright.sync_api import sync_playwright with sync_playwright() as playwright: browser = playwright.chromium.launch(headless=False) context = browser.new_context( http_credentials={ "username": "admin", "password": "admin" } ) page = context.new_page() page.goto(url="http://the-internet.herokuapp.com/basic_auth") context.close() browser.close() -
Using the
AuthorizationHTTP header (however, there are other authentication types):import base64 from playwright.sync_api import sync_playwright def generate_basic_authentication_header_value(username: str, password: str) -> str: token = base64.b64encode("{}:{}".format(username, password).encode("utf-8")).decode("ascii") return "Basic {}".format(token) with sync_playwright() as playwright: browser = playwright.chromium.launch(headless=False) context = browser.new_context( extra_http_headers={ "Authorization": generate_basic_authentication_header_value("admin", "admin"), } ) page = context.new_page() page.goto(url="http://the-internet.herokuapp.com/basic_auth") context.close() browser.close()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论