selenium wait_for_request
时间: 2023-10-09 20:09:21 浏览: 43
Selenium provides a method called `wait_for_request` which waits for a specific network request to be made or completed. This method is useful when testing web applications that use AJAX or other asynchronous requests.
Here is an example of how to use `wait_for_request`:
```python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# wait for a specific network request to be made
def wait_for_request(driver, url):
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, f'[src*="{url}"]')))
# navigate to a page that makes an AJAX request
driver.get('https://example.com')
# wait for the AJAX request to complete
wait_for_request(driver, 'ajax_request.js')
# continue with the test
```
In this example, we define a `wait_for_request` function that takes a `driver` object and a `url` parameter. The function uses the `WebDriverWait` class to wait for an element with a CSS selector that matches the URL of the network request. Once the element is present, the function returns and the test can continue.