簡介:
官網地址:Playwright | Playwright .NET
一:Playwright安裝及介紹
Playwright安裝及介紹 - 簡書 (jianshu.com)
1.手動安裝 python https://www.python.org/
安裝之後 按需升級爲最新 python.exe -m pip install --upgrade pip
2.安裝 playwright-python 依賴庫 pip install playwright
3.安裝Chromium、Firefox、WebKit等瀏覽器的驅動文件(內置瀏覽器) python -m playwright install
4.發佈之後 在發佈文件夾下會自動生成 .playwright 文件夾 需要一起發佈並安裝驅動後才能執行
在 .playwright\node\win32_x64\playwright.cmd install
二:微軟官方接口
三:常用操作積累
/// <summary>
/// 無頭瀏覽器全局變量
/// </summary>
private static IPlaywright playWright = null;
private static Microsoft.Playwright.IBrowser playBrowser = null;
private static Microsoft.Playwright.IPage playPage = null;
//https://github.com/microsoft/playwright/issues/4046
if (playPage?.IsClosed ?? true)
{
playWright = await Playwright.CreateAsync();//創建playwright對象
//創建一個瀏覽器對象 以谷歌爲例,其它瀏覽器創建方式相同
//Firefox Chromium
playBrowser = await playWright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions()
{
Headless = false,
Channel = "chrome",//chrome firefox msedge
//Devtools = true,
ChromiumSandbox = true,
ExecutablePath = string.Empty, // 不指定瀏覽器可執行文件位置,會自動尋找 ms-playwright 下載的瀏覽器
Args = new[] { "--enable-automation=true", "--disable-blink-features=AutomationControlled", "--start-maximized" }, // 防止selenium被檢測
});
var content = await playBrowser.NewContextAsync(new BrowserNewContextOptions()
{
ViewportSize = ViewportSize.NoViewport,//這個對應上面的 --start-maximized 最大化生效
}
);
playPage = await content.NewPageAsync();
}
//爲頁面指定一個鏈接
await playPage.GotoAsync("https://www.baidu.com/");
//頁面存在當前元素 個數 用於判斷是否出現了某個元素
var isHas = await playPage.Locator("#J-userName").CountAsync();
填充內容的幾種方式 根據Id class xpath等
await playPage.FillAsync("#J-userName", "填充文本");
await playPage.FillAsync("input[id='J-password']", "填充文本");
await playPage.ClickAsync("a[id='J-login']");//點擊某個標籤
await page.WaitForSelectorAsync("xpath=//tr[datatran=\"G1939\"]");
//輸入 當有下拉的輸入不生效,可以再次輸入一個 空格,然後刪除空格即可
await playPage.TypeAsync("#fromStationText", reqTickets.Fs);
await playPage.TypeAsync("#fromStationText", " ");
await playPage.Keyboard.PressAsync("Backspace");
//獲取某個標籤的 相對定位,可以點下鼠標並移動,實現滑塊操作
var nodeslid = await playPage.Locator("#" + slidId).BoundingBoxAsync();
var x = nodeslid.X + nodeslid.Width / 2;
var y = nodeslid.Y + nodeslid.Height / 2;
await playPage.Mouse.MoveAsync(x, y);//移動鼠標
await playPage.Mouse.DownAsync();//按下鼠標
await playPage.Mouse.MoveAsync(x + 340, y, new MouseMoveOptions() { Steps = new Random().Next(100, 200) });//steps 完成此操作的步數,數值越大,速度越慢
await playPage.Mouse.UpAsync();//放開鼠標
//獲取瀏覽器打開的新頁籤,操作同當前頁籤一致 var paypage = playBrowser.Contexts[0].Pages[1]; 【***重點***】 //攔截獲取網絡請求迴應 //當請求的url包含某個url的時候,可以獲取完整的請求
var payUrl = await paypage.RunAndWaitForRequestAsync(async () =>
{
await paypage.ClickAsync("img[title='提交']");
}, request => request.Url.Contains("abc.baidu.com/updateord.htm"));
//獲取迴應
var payResData = await paypage.RunAndWaitForResponseAsync(async () =>
{
await paypage.ClickAsync("img[title='支付寶']");
}, request => request.Url.Contains("mrmoaprod.alipay.com/exterfaceAssign.htm"));
//針對數據已經填充好了的 下拉列表可以直接賦值
await playPage.SelectOptionAsync("#select-merch", "下拉的key值");
常見問題積累:
1、ClickAsync 沒反應:
需要考慮是不是有多個元素,部分元素是隱藏不可見的,結合Locator 可以判斷是否有多個,針對可視的點擊 加上 :visible 如 await playPage.ClickAsync("a[id=‘1A’]:visible");
聲明:收集來源於網絡,技術僅用於學習交流,作者對讀者使用當前技術產生的後果不負責任。