
Selenium의 Python 바인딩을 확장하여 브라우저가 만든 요청을 검사할 수 있는 기능을 제공합니다.
Selenium Wire는 더 이상 유지 관리되지 않습니다. 지원해 주시고 기여해 주신 모든 분들께 감사드립니다.
Selenium Wire는 Selenium의 <https://www.selenium.dev/documentation/en/>_ Python 바인딩을 확장하여 브라우저에서 이루어지는 기본 요청에 접근할 수 있게 해 줍니다. Selenium을 사용할 때와 동일한 방식으로 코드를 작성하되, 요청과 응답을 검사하고 즉석에서 변경할 수 있는 추가 API를 제공받습니다.
.. image:: https://github.com/wkeeling/selenium-wire/workflows/build/badge.svg :target: https://github.com/wkeeling/selenium-wire/actions
.. image:: https://codecov.io/gh/wkeeling/selenium-wire/branch/master/graph/badge.svg :target: https://codecov.io/gh/wkeeling/selenium-wire
.. image:: https://img.shields.io/badge/python-3.7%2C%203.8%2C%203.9%2C%203.10-blue.svg :target: https://pypi.python.org/pypi/selenium-wire
.. image:: https://img.shields.io/pypi/v/selenium-wire.svg :target: https://pypi.python.org/pypi/selenium-wire
.. image:: https://img.shields.io/pypi/l/selenium-wire.svg :target: https://pypi.python.org/pypi/selenium-wire
.. image:: https://pepy.tech/badge/selenium-wire/month :target: https://pepy.tech/project/selenium-wire
간단한 예제~~~~~~~~~~~~~~
.. code:: python
from seleniumwire import webdriver # Import from seleniumwire
# Create a new instance of the Chrome driver
driver = webdriver.Chrome()
# Go to the Google home page
driver.get('https://www.google.com')
# Access requests via the `requests` attribute
for request in driver.requests:
if request.response:
print(
request.url,
request.response.status_code,
request.response.headers['Content-Type']
)
Prints:
.. code:: bash
https://www.google.com/ 200 text/html; charset=UTF-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png 200 image/png
https://consent.google.com/status?continue=https://www.google.com&pc=s×tamp=1531511954&gl=GB 204 text/html; charset=utf-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 200 image/png
https://ssl.gstatic.com/gb/images/i2_2ec824b0.png 200 image/png
https://www.google.com/gen_204?s=webaft&t=aft&atyp=csi&ei=kgRJW7DBONKTlwTK77wQ&rt=wsrt.366,aft.58,prt.58 204 text/html; charset=UTF-8
...
Features
* Pure Python, user-friendly API
* HTTP and HTTPS requests captured
* Intercept requests and responses
* Modify headers, parameters, body content on the fly
* Capture websocket messages
* HAR format supported
* Proxy server support
Compatibilty
Table of Contents
- `설치`_
* `브라우저 설정`_
* `OpenSSL`_
- `Webdriver 생성`_
- `요청 접근`_
- `요청 객체`_
- `응답 객체`_
- `요청 및 응답 가로채기`_
* `예: 요청 헤더 추가`_
* `예: 기존 요청 헤더 교체`_
* `예: 응답 헤더 추가`_
* `예: 요청 매개변수 추가`_
* `예: POST 요청 본문의 JSON 업데이트`_
* `예: 기본 인증`_
* `예: 요청 차단`_
* `예: 응답 모킹`_
* `인터셉터 설정 해제`_
- `요청 캡처 제한`_
- `요청 저장소`_
* `메모리 내 저장소`_
- `프록시`_
* `SOCKS`_
* `동적 전환`_
- `봇 탐지`_
- `인증서`_
* `자체 인증서 사용`_
- `모든 옵션`_
- `라이선스`_
설치~~~~~~~~~~~~
Install using pip:
.. code:: bash
pip install selenium-wire
If you get an error about not being able to build cryptography you may be running an old version of pip. Try upgrading pip with ``python -m pip install --upgrade pip`` and then re-run the above command.
Browser Setup
-------------
No specific configuration should be necessary except to ensure that you have downloaded the relevent webdriver executable for your browser and placed it somewhere on your system PATH.
- `Download <https://sites.google.com/chromium.org/driver/>`__ webdriver for Chrome
- `Download <https://github.com/mozilla/geckodriver/>`__ webdriver for Firefox
- `Download <https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/>`__ webdriver for Edge
OpenSSL
-------
Selenium Wire requires OpenSSL for decrypting HTTPS requests. This is probably already installed on your system (you can check by running ``openssl version`` on the command line). If it's not installed you can install it with:
**Linux**
.. code:: bash
# For apt based Linux systems
sudo apt install openssl
# For RPM based Linux systems
sudo yum install openssl
# For Linux alpine
sudo apk add openssl
**MacOS**
.. code:: bash
brew install openssl
**Windows**
No installation is required.
Creating the Webdriver
webdriver를 seleniumwire 패키지에서 가져와야 합니다:
.. code:: python
from seleniumwire import webdriver
그런 다음 Selenium을 직접 사용할 때와 동일하게 webdriver를 인스턴스화하면 됩니다. 원하는 기능이나 브라우저별 옵션(예: 실행 파일 경로, 헤드리스 모드 등)을 전달할 수 있습니다. Selenium Wire에는 seleniumwire_options 속성으로 전달할 수 있는 자체 옵션_도 있습니다.
.. code:: python
# Create the driver with no options (use defaults)
driver = webdriver.Chrome()
# Or create using browser specific options and/or seleniumwire_options options
driver = webdriver.Chrome(
options = webdriver.ChromeOptions(...),
seleniumwire_options={...}
)
.. _own options: #all-options
webdriver의 하위 패키지에 대해서는 계속해서 selenium에서 직접 가져와야 합니다. 예를 들어 WebDriverWait를 가져오려면:
.. code:: python
# Sub-packages of webdriver must still be imported from `selenium` itself
from selenium.webdriver.support.ui import WebDriverWait
원격 Webdriver
Selenium Wire는 원격 webdriver 클라이언트 사용을 제한적으로 지원합니다. 원격 webdriver 인스턴스를 생성할 때 Selenium Wire가 실행 중인 머신(또는 컨테이너)의 호스트 이름 또는 IP 주소를 지정해야 합니다. 이렇게 하면 원격 인스턴스가 요청 및 응답을 Selenium Wire로 다시 통신할 수 있습니다.
.. code:: python
options = {
'addr': 'hostname_or_ip' # Address of the machine running Selenium Wire. Explicitly use 127.0.0.1 rather than localhost if remote session is running locally.
}
driver = webdriver.Remote(
command_executor='http://www.example.com',
seleniumwire_options=options
)
브라우저가 실행 중인 머신이 Selenium Wire가 실행 중인 머신과 통신하기 위해 다른 주소를 사용해야 한다면 브라우저를 수동으로 구성해야 합니다. 이 이슈 <https://github.com/wkeeling/selenium-wire/issues/220>_ 에 자세한 내용이 나와 있습니다.
요청에 접근하기~~~~~~~~~~~~~~~~~~
Selenium Wire captures all HTTP/HTTPS traffic made by the browser [1]_. The following attributes provide access to requests and responses.
driver.requests
The list of captured requests in chronological order.
driver.last_request
Convenience attribute for retrieving the most recently captured request. This is more efficient than using driver.requests[-1].
driver.wait_for_request(pat, timeout=10)
This method will wait until it sees a request matching a pattern. The pat attribute will be matched within the request URL. pat can be a simple substring or a regular expression. Note that driver.wait_for_request() doesn't make a request, it just waits for a previous request made by some other action and it will return the first request it finds. Also note that since pat can be a regular expression, you must escape special characters such as question marks with a slash. A TimeoutException is raised if no match is found within the timeout period.
For example, to wait for an AJAX request to return after a button is clicked:
.. code:: python
# Click a button that triggers a background request to https://server/api/products/12345/
button_element.click()
# Wait for the request/response to complete
request = driver.wait_for_request('/api/products/12345/')
driver.har
A JSON formatted HAR archive of HTTP transactions that have taken place. HAR capture is turned off by default and you must set the enable_har option_ to True before using driver.har.
driver.iter_requests()
Returns an iterator over captured requests. Useful when dealing with a large number of requests.
driver.request_interceptor
Used to set a request interceptor. See Intercepting Requests and Responses_.