软件测试|web自动化测试神器playwright教程(三十二)

举报
霍格沃兹测试开发 发表于 2023/08/07 18:25:47 2023/08/07
【摘要】 前言我们之前介绍过,我们在本地安装playwright时,默认会下载chromium,firefox 和 webkit浏览器,当然playwright不只支持下载这3个浏览器,还支持下载chrome、chrome-beta、msedge、msedge-beta、msedge-dev浏览器。 下载浏览器在之前的教程中,我们介绍过,运行playwright install命令会自动安装chro...

image.png

前言

我们之前介绍过,我们在本地安装playwright时,默认会下载chromium,firefox 和 webkit浏览器,当然playwright不只支持下载这3个浏览器,还支持下载chrome、chrome-beta、msedge、msedge-beta、msedge-dev浏览器。

下载浏览器

在之前的教程中,我们介绍过,运行playwright install命令会自动安装chromium,firefox和webkit浏览器,如果我们想要安装其他的浏览器,在安装之前,我们可以运行playwright install --help查看支持的浏览器,示例如下:

playwright install --help

Usage: playwright install [options] [browser...]

ensure browsers necessary for this version of Playwright are installed

Options:
  --with-deps  install system dependencies for browsers
  --dry-run    do not execute installation, only print information
  --force      force reinstall of stable browser channels
  -h, --help   display help for command


Examples:
  - $ install
    Install default browsers.

  - $ install chrome firefox
    Install custom browsers, supports chromium, chrome, chrome-beta, msedge, msedge-beta, msedge-dev, firefox, webkit.

从输出信息来看,支持的浏览器有:chromium, chrome, chrome-beta, msedge, msedge-beta, msedge-dev, firefox, webkit

注:安装指定的浏览器, 如果本机已经安装过了,就不会再安装了

命令如下:

playwright install chrome

playwright install msedge

安装Chrome和edge时,这两个浏览器将被安装在默认位置,具体位置视操作系统而定。

示例如下:

playwright install chrome

Failed to install browsers
Error:
╔═════════════════════════════════════════════════════════════════╗
║ ATTENTION: "chrome" is already installed on the system!         ║
║                                                                 ║
║ "chrome" installation is not hermetic; installing newer version ║
║ requires *removal* of a current installation first.             ║
║                                                                 ║
║ To *uninstall* current version and re-install latest "chrome":  ║
║                                                                 ║
║ - Close all running instances of "chrome", if any               ║
║ - Use "--force" to install browser:                             ║
║                                                                 ║
║     playwright install --force chrome                           ║
║                                                                 ║
║ <3 Playwright Team                                              ║
╚═════════════════════════════════════════════════════════════════╝
--------------------------------------------------------------------------------

playwright install msedge

Failed to install browsers
Error:
╔═════════════════════════════════════════════════════════════════╗
║ ATTENTION: "msedge" is already installed on the system!         ║
║                                                                 ║
║ "msedge" installation is not hermetic; installing newer version ║
║ requires *removal* of a current installation first.             ║
║                                                                 ║
║ To *uninstall* current version and re-install latest "msedge":  ║
║                                                                 ║
║ - Close all running instances of "msedge", if any               ║
║ - Use "--force" to install browser:                             ║
║                                                                 ║
║     playwright install --force msedge                           ║
║                                                                 ║
║ <3 Playwright Team                                              ║
╚═════════════════════════════════════════════════════════════════╝

如上所示,因为本地已经安装了Chrome和Edge浏览器,所以playwright安装会报错,提示浏览器已经安装。

根据报错提示,我们也可以通过运行playwright install --force browser来强制安装浏览器

playwright install --force chrome

Downloading Google Chrome
Installing Google Chrome

ProductVersion   FileVersion      FileName
--------------   -----------      --------
113.0.5672.127   113.0.5672.127   C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

打开指定浏览器

如果不加参数,我们运行chromium.launch() 不带 channel 参数默认打开的是 chromium 浏览器

from playwright.sync_api import sync_playwright


with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)
    page = browser.new_page()

    page.goto("https://www.baidu.com/")

我们都知道,Chrome和edge都是使用的chromium内核,因此我们只需要再加一个chanel参数即可,如果我们要打开Chrome浏览器,则参数为channel="chrome",示例如下:

from playwright.sync_api import sync_playwright


with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False, channel="chrome")
    page = browser.new_page()

    page.goto("https://www.baidu.com/")
    page.pause()

运行脚本,浏览器界面如下:

image.png

同理,要打开edge浏览器,添加channel="msedge"参数即可,代码如下:

from playwright.sync_api import sync_playwright


with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False, channel="msedge")
    page = browser.new_page()

    page.goto("https://www.baidu.com/")
    page.pause()

运行脚本,打开的浏览器页面如下:

image.png

总结

本文主要介绍了playwright对各浏览器的下载,以及启动不同浏览器的方法,我们需要注意启动Chrome或者edge浏览器时,需要加上chanel参数。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。