在Playwright中,没有Element这个概念,只有Page的概念,Page不仅仅指的是某个页面,例如页面间的跳转等,还包含了所有元素、事件的概念,所以我们包括定位元素、页面转向,都是基于Page操作的。
一、进入一个URL
page.goto("https://example.com")
二、截图
page.screenshot(path="screenshot.png")
三、监听Iframe加载(监听页面事件)
page.on("frameattached", handler)
四、定位到含有指定文字的元素
(返回一个Locator)
page.get_by_text("test") # 模糊匹配
page.get_by_text("test", exact=True) # 精准匹配
五、返回上一页
page.go_back()
page.go_back(**kwargs)
六、前往下一页
page.go_forward()
page.go_forward(**kwargs)
七、根据选择器定位元素(CSS\Xpath)
(返回一个Locator)
page.locator(selector)
page.locator(selector, **kwargs)