一.在线安装
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!不用写一行代码!