Integrate Selenium Wire for Chrome and Firefox
v1.15.0 - 2022.03.30
Overview
Selenium-Wire was proposed by Discussion #239. I was exploring this as an option and saw that we would be able to integrate with a few changes across PyleniumConfig and webdriver_factory.
What is Selenium-Wire?
From their GitHub page:
Selenium Wire extends Selenium's Python bindings to give you access to the underlying requests made by the browser. You author your code in the same way as you do with Selenium, but you get extra APIs for inspecting requests and responses and making changes to them on the fly.
Now you can use it with Pylenium!
The good news is that it's been integrated! 🎉 The bad news of the current implementation is that the Pylenium class is unaware if it's using a selenium-wire driver, so it provides no autocomplete or intellisense. We need to find a way to provide the correct type hinting so we do provide autocomplete for our users.
For example, if you set the browser to "chrome", you will get a selenium-wire driver and can do something like this:
# Go to the Google home page
py.visit("https://google.com")
# Access requests via the `requests` attribute
for request in py.webdriver.requests:
if request.response:
print(
request.url,
request.response.status_code,
request.response.headers['Content-Type']
)but when you type py.webdriver., you won't see requests as an available option even though it does exist and works.
Known Issues
- No intellisense or autocomplete as shown in the example above
- Selenium-Wire only works for local Chrome and Firefox drivers. Remote is not yet supported
selenium-wire has a lot of power and configuration, so make sure you refer to their docs for more details.
Add
pylenium.json now has a seleniumwire_options field where you can set specific wire options! Refer to selenium-wire docs for more details and examples about their options.
Fixes
WebDriverManager needed to be updated to the latest because their older versions are broken for different browsers, especially with Selenium 4.