Playwright設(shè)置base_url的三種方式
方式一:playwright創(chuàng)建new_context()
- 在new_context(base_url=‘’)中指定base_url
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(base_url="http://localhost:8080")
示例代碼:
import json
from playwright.sync_api import Playwright, sync_playwright
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(base_url="http://localhost:8080")
page = context.new_page()
page.goto("/login")
page.get_by_role("textbox", name="用戶名").click()
page.get_by_role("textbox", name="用戶名").fill("admin")
page.get_by_role("textbox", name="密碼").click()
page.get_by_role("textbox", name="密碼").fill("admin123")
page.get_by_role("button", name="登錄").click()
# 等待頁(yè)面跳轉(zhuǎn),作為登錄成功的標(biāo)準(zhǔn)
page.wait_for_url(url='http://localhost:8080/index')
# page.pause()
# 保存storage state 到指定的文件
# 使用該方法前,需要確認(rèn)網(wǎng)站cookie的失效時(shí)間
storage = context.storage_state(path='../../auth/ry_auto.json')
print(storage)
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
方式二:使用插件pytest-playwright
pip install pytest-playwright
- 在
pytest.ini配置文件中,使用addopts屬性在命令行中添加參數(shù)--base-url http://localhost:8080
pytest.ini配置文件如下:
[pytest] addopts = -s --base-url http://localhost:8080
方式三:使用插件pytest-base-url
- 該插件在
pytest.ini配置文件中,添加了一個(gè)行屬性,base_url。
pip install pytest-base-url
pytest.ini配置文件如下:
[pytest] base_url = http://localhost:8080
到此這篇關(guān)于Playwright設(shè)置base_url的三種方式的文章就介紹到這了,更多相關(guān)Playwright設(shè)置base_url內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在python的類中動(dòng)態(tài)添加屬性與生成對(duì)象
這篇文章給大家介紹了如何在python的類中動(dòng)態(tài)添加屬性和生成對(duì)象,文中通過(guò)幾個(gè)方面來(lái)進(jìn)行介紹,對(duì)這感興趣的朋友們可以學(xué)習(xí)學(xué)習(xí)。2016-09-09
Python實(shí)現(xiàn)簡(jiǎn)單字典樹(shù)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單字典樹(shù)的方法,實(shí)例分析了Python字典樹(shù)的定義、實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2016-04-04
python中pymysql的executemany使用方式
這篇文章主要介紹了python中pymysql的executemany使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python使用BeautifulSoup庫(kù)解析HTML基本使用教程
這篇文章主要介紹了Python使用BeautifulSoup庫(kù)解析HTML基本使用教程,文中主要對(duì)其適合于制作爬蟲(chóng)方面的特性進(jìn)行了解析,需要的朋友可以參考下2016-03-03
Python grequests模塊使用場(chǎng)景及代碼實(shí)例
這篇文章主要介紹了Python grequests模塊使用場(chǎng)景及代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Python簡(jiǎn)繁體轉(zhuǎn)換的簡(jiǎn)單實(shí)現(xiàn)步驟
工作中需要將繁體中文轉(zhuǎn)換成簡(jiǎn)體中文上網(wǎng)找了些資料,下面這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)簡(jiǎn)繁體轉(zhuǎn)換的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python通過(guò)調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能示例
這篇文章主要介紹了Python通過(guò)調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能,結(jié)合實(shí)例形式分析了基于Python實(shí)現(xiàn)的有道翻譯api調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

