python02 playwright1

一.在线安装

r"""
【1】pycharm_File_settings安装模块 如下是pip安装:
F:\Program Files (x86)\jetbrains\python3.8.9\Scripts 文件地址栏输入cmd
pip show playwright
pip check playwright
pip uninstall -y playwright
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ playwright
pip show playwright
【2】
如出现错误 playwright._impl._api_types.Error: Executable doesn‘t exist at
C:\Users\Administrator\AppData\Local\ms-playwright\chromium-1080\chrome-win\chrome.exe

F:\Program Files (x86)\jetbrains\python3.8.9\Scripts 输入cmd
playwright install

F:\Program Files (x86)\jetbrains\python3.8.9\Scripts>playwright install
Removing unused browser at C:\Users\Administrator\AppData\Local\ms-playwright\chromium-1045
Removing unused browser at C:\Users\Administrator\AppData\Local\ms-playwright\ffmpeg-1008
Downloading Chromium 117.0.5938.62 (playwright build v1080)
谷歌浏览器地址栏输入:chrome://version/
【3】
F:\Program Files (x86)\jetbrains\python3.8.9\Scripts> playwright install
playwright install chromium
【4】D目录=F:\Program Files (x86)\jetbrains\python3.8.9\Scripts
F:\Program Files (x86)\jetbrains\python3.8.9>python -m pip install --upgrade pip
D目录> pip download playwright -d ./home/playwright (相对路径)
D目录> pip download playwright -d /home/playwright (默认本磁盘绝对路径)
【5】
如果报错DLL load failed while import _greenlet:找不到指定的模块
D目录>  pip download msvc-runtime -d /home/msvc-runtime
"""
import time
from playwright.sync_api import sync_playwright
def demo1():  #网页未打开 连接本机chrome
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        time.sleep(3)
        browser.close()

def demo2():  #网页仍未打开 连接本机chrome 生成网页对象
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        time.sleep(3)
        context.close()
        browser.close()

def demo3():  #打开空白网页
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        time.sleep(3)
        page.close()
        context.close()
        browser.close()

def demo4(): #同一个网页窗口跳转网页  网页向后向前
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        time.sleep(1)
        page.goto("http://www.sina.com.cn/")
        time.sleep(1)
        page.go_back()
        time.sleep(1)
        page.go_forward()
        time.sleep(1)
        page.close()
        context.close()
        browser.close()

def demo5(): #no_viewport 窗口变成屏幕一半 是否能禁用固定窗口
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context(no_viewport=True)
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        time.sleep(3)
        page.close()
        context.close()
        browser.close()

二. win7离散安装

离线安装 playwright 环境

r"""  node.exe(低版本playwright windows7才支持) 浏览器(适用playwright版本) 
【0】F:\Program Files (x86)\jetbrains\python3.8.9>python -m pip install --upgrade pip
【1】win7需要安装低版本playwright  高版本出现如下错误:
Node.js is only supported on Windows 8.1, Windows Server 2012 R2, or higher

【2】不使用该方案的虚拟环境(venv) 直接使用已安装python3.8.9的解释器
1. F:\Program Files (x86)\jetbrains\python3.8.9\Scripts 该目录(简称D目录)下打开cmd
pip download playwright==1.15.3 -d f:/home/playwright1.15.3 (下载低版本 win7支持)
2.将f:/home/playwright1.15.3目录内文件复制到  D目录/home/playwright1.15.3
3.D目录/home/a1.txt  生成a1.txt内容如下:

greenlet==2.0.2
playwright==1.15.3
pyee==11.0.0
typing_extensions==4.8.0
websockets==11.0.3

4.安装低版本 卸载再重新安装
pip uninstall -y playwright (卸载)
pip install --no-index --find-links=.\home\playwright1.15 -r .\home\a1.txt (重新安装)

【3】安装成功后 查看如下目录 node.exe适用playwright的版本
F:\Program Files (x86)\jetbrains\python3.8.9\Lib\site-packages\playwright\driver\node.exe

【4】程序运行仍有可能出错 需要适用的浏览器
F:\Program Files (x86)\jetbrains\python3.8.9\Scripts  playwright install
F:\Program Files (x86)\jetbrains\python3.8.9>python -m playwright install (model同上)
playwright install (报错 显示谷歌版本 适合playwright-1.15.3版本的 默认安装多个浏览器)
C:\Users\Administrator\AppData\Local\ms-playwright(查看该目录 默认安装多个浏览器)
C:\Users\Administrator\AppData\Local\ms-playwright\chromium-920619 目录

【5】再卸载重新安装 只安装谷歌浏览器
C:\Users\Administrator\AppData\Local\ms-playwright (仅删除已安装浏览器目录)
F:\Program Files (x86)\jetbrains\python3.8.9\Scripts  playwright install chromium
C:\Users\Administrator\AppData\Local\ms-playwright\chromium-920619 (新增浏览器目录)

【6】如果报错DLL load failed while import _greenlet:找不到指定的模块
pip download msvc-runtime -d /home/msvc-runtime
"""
import time
from playwright.sync_api import sync_playwright
def demo1():
    with sync_playwright() as p: ##使用本机安装的谷歌浏览器
        browser = p.chromium.launch(channel='chrome', headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto('http://www.baidu.com')
        time.sleep(2)
        context.close()
        browser.close()

def demo2(): #chrome://version/ 显示路径 executable_path本机浏览器的安装地址
    c=r"C:\Users\Administrator\AppData\Local\google\Chrome\Application\chrome.exe"
    with sync_playwright() as p: ##使用本机安装的谷歌浏览器
        browser = p.chromium.launch(headless=False,executable_path=c)
        context = browser.new_context()
        page = context.new_page()
        page.goto('http://www.baidu.com')
        time.sleep(2)
        context.close()
        browser.close()

def demo3(): #playwright install自行安装的谷歌浏览器
    c = r"C:\Users\Administrator\AppData\Local\ms-playwright\chromium-920619"
    c+=r"\chrome-win\chrome.exe"
    with sync_playwright() as p:  ##使用本机安装的谷歌浏览器
        browser = p.chromium.launch(headless=False, executable_path=c)
        context = browser.new_context()
        page = context.new_page()
        page.goto('http://www.baidu.com')
        time.sleep(2)
        context.close()
        browser.close()

二. 代码测试

(一) 代码1

import time
from playwright.sync_api import sync_playwright

def demo1():  #网页未打开 连接本机chrome
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        time.sleep(3)
        browser.close()

def demo2():  #网页仍未打开 连接本机chrome 生成网页对象
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        time.sleep(3)
        context.close()
        browser.close()

def demo3():  #打开空白网页
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        time.sleep(3)
        page.close()
        context.close()
        browser.close()

def demo4(): #同一个网页窗口跳转网页  网页向后向前
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        time.sleep(1)
        page.goto("http://www.sina.com.cn/")
        time.sleep(1)
        page.go_back()
        time.sleep(1)
        page.go_forward()
        time.sleep(1)
        page.close()
        context.close()
        browser.close()

def demo5(): #no_viewport 窗口变成屏幕一半 是否能禁用固定窗口
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context(no_viewport=True)
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        time.sleep(3)
        page.close()
        context.close()
        browser.close()

(二) 代码2

import time
from playwright.sync_api import sync_playwright

def demo1(): #初使化窗口最大化
    playwright = sync_playwright().start()

    browser = playwright.chromium.launch(headless=False, 
        args=["--start-maximized"])

    context = browser.new_context(no_viewport=True) #窗口一半不起作用了
    page = context.new_page()
    page.goto(r"http:\\www.baidu.com")
    time.sleep(3)
    page.close()
    context.close()
    browser.close()

class C1(): #初使窗口的大小
    def setup_method(self) -> None:
        self.playwright = sync_playwright().start()
        self.browser = self.playwright.chromium.launch(headless=False)

        self.context = self.browser.new_context(
           viewport={'width': int(1800*0.5), 'height': 1024}) #1800 1024

        self.page = self.context.new_page()
        time.sleep(3)
        page1=self.context.new_page()
        page1.goto("http://www.baidu.com")
        time.sleep(3)
        self.page.close()
        time.sleep(3)
        page1.close()
        self.context.close()
        self.browser.close()
#C1().setup_method()

def demo2():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("https://www.baidu.com/")
        time.sleep(1)  # 防止百度 图形验证

        page.locator("#kw").click()
# 浏览器检查  <input id="kw" name="wd" class="s_ipt"
# value="" maxlength="255" autocomplete="off">
        time.sleep(1)
        page.locator("#kw").fill("惠州学院")
        time.sleep(1)
        page.locator("#kw").press("Enter")
        time.sleep(1)
        page_data = page.content()
        with open("a1.html",mode="w",encoding="utf8")as f:
            f.write(page_data)
        print(page.url)
        time.sleep(1000)  
# 足够时间 chrome浏览器(右键)_检查_Elements_找到对应的html标签
        context.close()
        browser.close()

(三) 多个窗口

from playwright.sync_api import Playwright, sync_playwright
import time

def demo1():
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(headless=False, args=["--start-maximized"])

    context0 = browser.new_context()
    page0 = context0.new_page()
    page0.goto("http://www.baidu.com")
    page0.close()
    context0.close()
    time.sleep(2)

    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.baidu.com/")
    print("【1】", page.title(), page.url)

    time.sleep(2)
    page.get_by_role("link", name="新闻").click()
    time.sleep(2)
    print("【2】网页标题和链接未变(why):", page.title(), page.url)

    page.close()
    context.close()
    browser.close()

def demo2():
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page1 = context.new_page()
    page1.goto("https://www.baidu.com/")
    print("【1】", page1.title(), page1.url)

    with page1.expect_popup() as page_info:
        time.sleep(1)
        page1.get_by_role("link", name="新闻").click()
    page2 = page_info.value
    print("【2】",page2,page2.url,end="\t")
    time.sleep(2) #【可能报错】标题还没有显示出来,则报错
    print(page2.title())

    page1.close()
    time.sleep(2)
    page2.close()
    time.sleep(2)
    print("【3】 page1 page2 已关闭" )
    time.sleep(3)
    context.close()
    browser.close()

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.baidu.com/")
    print("【1】",page.title(), page.url)

    with page.expect_popup() as page1_info:
        time.sleep(1)
        page.get_by_role("link", name="新闻").click()
    page1 = page1_info.value
    time.sleep(2)
    print("【2】",page1.title(), page1.url)

    with page.expect_popup() as page2_info:
        page.get_by_role("link", name="贴吧").click()
    page2 = page2_info.value
    time.sleep(2)
    print("【3】",page2.title(), page2.url)

    with page.expect_popup() as page3_info:
        page.get_by_role("link", name="hao123").click()
    page3 = page3_info.value
    time.sleep(2)
    print("【4】",page3.title(), page3.url)

    time.sleep(1)
    page1.close()
    time.sleep(1)
    page2.close()
    time.sleep(1)
    page3.close()
    time.sleep(2)

    context.close()
    browser.close()

def demo3():
    with sync_playwright() as playwright:
        run(playwright)
demo3()

微软开源最强Python自动化神器Playwright!不用写一行代码!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,717评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,501评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,311评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,417评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,500评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,538评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,557评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,310评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,759评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,065评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,233评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,909评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,548评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,172评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,420评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,103评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,098评论 2 352

推荐阅读更多精彩内容