简介:
Playwright是由微软公司2020年初发布的新一代自动化测试工具,相较于目前最常用的Selenium,它仅用一个API即可自动执行Chromium、Firefox、WebKit等主流浏览器自动化操作,并同时支持以无头模式、有头模式运行。官网地址: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");
声明:收集来源于网络,技术仅用于学习交流,作者对读者使用当前技术产生的后果不负责任。
标签:知识点,playwright,浏览器,--,await,Playwright,var,playPage From: https://www.cnblogs.com/flish/p/17275791.html