Several methods commonly used in playwright (in synchronous mode)

Several methods commonly used in playwright (in synchronous mode)

a brief introdction

In the Python environment, Playwright provides a relatively complete synchronous API, and developers can choose to use the asynchronous API or the synchronous API according to their preferences. The following is an introduction to Playwright's common synchronization methods.

specific method

1. page.goto(url[, options])

Like the asynchronous API, this method is used to load and jump to the specified URL. It accepts a URL parameter and optional options parameter. Commonly used option parameters include:

  • wait_until: Specifies the page loading state, the default is load. Can be set to domcontentloadedor networkidle0etc.

Here's an example using page.gotothe method :

page.goto('https://www.example.com')
page.goto('https://www.example.com', wait_until='networkidle0') # 等待网络空闲状态

2. page.wait_for_selector(selector[, options])

This method is used to wait for elements matching the specified CSS selector to appear on the page. It accepts a selector parameter and optional option parameters. Commonly used option parameters include:

  • visible: Specifies that the element must be visible, the default is False.
  • hidden: Specifies that the element must be hidden, the default is False.

Here's an example using page.wait_for_selectorthe method :

page.wait_for_selector('#myButton')
page.wait_for_selector('.my-div', visible=True) # 等待可见的元素

3. page.wait_for_load_state(state=None[, timeout=30000])

Similar to the asynchronous API, this method is used to wait for the loading status of the page, where statethe parameter can be one of the following values:

  • load: The page is fully loaded.
  • domcontentloaded: The DOM content has been loaded.
  • networkidle0: When the network is idle.
  • networkidle2: The number of network connections is stable and exceeds 500 milliseconds.

Here's an example using page.wait_for_load_statethe method :

page.goto('https://www.example.com')
page.wait_for_load_state('networkidle0') # 等待网络空闲状态

4. page.click(selector[, options])

Like the asynchronous API, this method simulates a click event on the element, accepting a selector parameter and an optional options parameter. The commonly used option parameters include:

  • button: The button that simulates a mouse click, the default is left.
  • click_count: The number of simulated clicks, the default is 1.

Here's an example using page.clickthe method :

page.click('#myButton')

5. page.type(selector, text, delay=None)

Similar to the asynchronous API, this method is used to enter text content in the specified element. Accepts a selector parameter and the input text content, and can also include the following optional options parameters:

  • delay: Simulates the delay time in milliseconds for each character input.

Here's an example using page.typethe method :

page.type('#myInput', 'Hello World!')

6. page.evaluate(pageFunction, *args)

Similar to the asynchronous API, this method executes the specified JavaScript function in the page context and returns the function's return value. It can execute custom JavaScript code in the browser. Here's an example using page.evaluatethe method :

result = page.evaluate('() => { return 1 + 2; }')

7. page.wait_for_function(pageFunction[, timeout=None, interval=None], *args)

Similar to the asynchronous API, this method continues to execute the specified JavaScript function until the function returns True. It can be used to wait for dynamically changing content on the page to appear, or to wait for an asynchronous operation to complete. Here's an example using page.wait_for_functionthe method :

page.wait_for_function('() => document.querySelector("#myButton").disabled === false')

8. page.screenshot(path=None, *, full_page=True, clip=None, quality=None, omit_background=False)

Similar to the asynchronous API, this method is used to take a screenshot of the current page and save it to the specified file. It accepts an optional option parameter, commonly used option parameters include:

  • path: Specify the file path and file name to save the screenshot.
  • full_page: Specifies whether the screenshot contains the entire page, the default is True.
  • clip: Specify the size and position of the screenshot.
  • quality: Specifies the quality of the screenshot.
  • omit_background: Specifies whether to ignore the background color.

Here's an example using page.screenshotthe method :

page.screenshot(path='example.png', full_page=True)
page.screenshot(clip={
    
    'x': 0, 'y': 0, 'width': 800, 'height': 600})

Summarize

The above are several methods commonly used by Playwright in the Python environment. Developers can choose the method that suits them according to their needs.

Supongo que te gusta

Origin blog.csdn.net/ekcchina/article/details/130572692
Recomendado
Clasificación