pip install playwright
python -m playwright install
# -*- coding: utf-8 -*-
# @Time : 2021/05/07 14:50
# @Author :
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)
# context = browser.contexts
# Open new page
page = browser.new_page()
page.goto("https://www.baidu.com/")
page.click('input[name="wd"]')
page.fill('input[name="wd"]', 'jingdong')
page.click('text="京东"')
# 当前页面内容
# html = page.content()
with page.expect_navigation():
with page.expect_popup() as popup_info:
# normalize-space这个方法就可以去除文本中的前后空格和回车
page.click("//a[normalize-space(.)='京东JD.COM官网 多快好省 只为品质生活']")
# popup_info.value
import time
time.sleep(10)
browser.close()
with sync_playwright() as playwright:
run(playwright)
参考:https://pypi.org/project/playwright/
参考:https://www.imooc.com/article/314264