playwright滑块
时间: 2023-09-14 17:03:13 浏览: 155
playwright-1.44.0
您可以使用Playwright库来自动下滑页面。以下是一个示例代码:
```python
from playwright.sync_api import sync_playwright
def scroll_down_page(page):
# 获取页面高度
page_height = page.evaluate('''() => {
return Math.max(
document.documentElement.clientHeight,
window.innerHeight || 0
);
}''')
# 模拟滚动到页面底部
page.evaluate(f'''(pageHeight) => {{
window.scrollTo({{top: pageHeight, behavior: 'smooth'}});
}}''', page_height)
# 初始化Playwright
with sync_playwright() as playwright:
# 启动浏览器并创建一个新的浏览器上下文
browser = playwright.chromium.launch()
context = browser.new_context()
# 创建一个新页面
page = context.new_page()
# 导航到目标页面
page.goto('https://www.example.com')
# 模拟多次下滑页面
for _ in range(3):
scroll_down_page(page)
# 关闭浏览器
browser.close()
```
这个示例代码使用Playwright打开一个浏览器,导航到指定页面,并模拟多次下滑页面。您可以根据需要修改循环的次数。请确保已经安装了Playwright库,可以使用`pip install playwright`命令进行安装。
阅读全文