playwright库 focus()方法python
时间: 2023-12-04 07:05:01 浏览: 169
注意力机制代码 python
`playwright` 库提供了 `focus()` 方法,用于将焦点设置为指定的元素。在 Python 中,可以使用以下代码来使用 `focus()` 方法:
```python
from playwright.sync_api import Playwright, sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto('https://www.example.com')
element = page.locator('input').first()
element.focus()
# Do something with the focused element
browser.close()
```
在此示例中,我们首先使用 `sync_playwright()` 启动了 Playwright,然后创建了一个新页面并导航到了 `https://www.example.com`。接下来,我们使用 `page.locator()` 方法找到了第一个 `<input>` 元素,并使用 `focus()` 方法将焦点设置为该元素。最后,我们关闭了浏览器。
阅读全文