Selenium-Injector
- Change proxy while running (auth supported)
- remotely contoll Chrome using websockets and extensions
Feel free to test my code!
Getting Started
Dependencies
- Python >= 3.7
- Chrome-Browser installed
- Selenium
Installing
- [Windows] Install Chrome-Browser
pip install selenium_injector
Example scripts
set proxy dynamically
from selenium_injector.webdriver import Chrome
driver = Chrome()
driver.injector.proxy.set_single(host="example_host.com", port=143, password="password", username="user-1")
driver.get("https://whatismyipaddress.com/")
driver.injector.proxy.clear()
driver.quit()
Don't forget to execute
driver.quit()
in the End. Else-wise your temporary folder will get flooded! and it keeps running
use events
from selenium_injector.webdriver import Chrome
import json
driver = Chrome()
driver.get("chrome://version")
t = driver.injector.socket.js.types
event_id = driver.injector.socket.make_event_id()
user = driver.injector.any_user
driver.injector.socket.exec(t.list([
t.set_event_id(event_id),
t.exec(
t.path("chrome.webRequest.onCompleted.addListener"),
args=[t.event_callback(), t.value({"urls": ["<all_urls>"]})]
)
]), user=user, max_depth=1)
event = driver.injector.socket.event(event_id, user=user)
for e in event: # will block forever
e = json.loads(e)
data = e["result"][0]
time = e["t"]
print(time + "\n", data['url'])
warning: as driver.quit() isn't called in this example, it will leave files in your temp directories
modify network requests
note: this is only experimental yet (not included in pypi package)
example script
from selenium_injector.webdriver import Chrome
driver = Chrome()
# modify headers
driver.injector.declarativeNetRequest.update_headers({"test": "test_2", "sec-ch-ua-platform": "Android"})
rules = driver.injector.declarativeNetRequest.dynamic_rules
headers = driver.injector.declarativeNetRequest._headers
driver.get("https://httpbin.org/headers")
input("press ENTER to continue")
# block images
driver.injector.declarativeNetRequest.update_block_on(resource_types=["image"])
driver.get("https://www.wikimedia.org/")
input("press ENTER to exit")
driver.quit()
use chrome-developer-protocoll
note: this is only experimental yet (not included in pypi package)
example script
import json
from selenium_injector.webdriver import Chrome
driver = Chrome()
dbg = driver.injector.debugger
dbg.attach()
dbg.execute("Console.enable")
events = dbg.on_event()
driver.execute_script("console.log('Hello World!')")
for event in events:
event = json.loads(event)
result = event["result"]
time = event["t"]
if result[1] == 'Console.messageAdded':
message_text = result[2]["message"]["text"]
print(time, message_text)
break
driver.quit()
execute script within tab
note: this is only experimental yet (not included in pypi package)
from string
from selenium_injector.webdriver import Chrome
driver = Chrome(injector_options={"mv2":True, "mv3":True})
driver.get("https://www.wikipedia.org/")
# result only returned with mv2 extension enabled
results = driver.injector.tabs.eval_str(
'''
console.log(window);
navigator.userAgent
''', tab_id=driver.injector.tabs.active_tab["id"])
print(results[0])
driver.quit()
with types, always returns resolves promises with MV3 automatically
from selenium_injector.webdriver import Chrome
driver = Chrome(injector_options={"mv3":True})
driver.get("https://www.wikipedia.org/")
t = driver.injector.socket.js.types
results = driver.injector.tabs.exec(t.exec(t.path("fetch"), args=[t.value("https://www.wikipedia.org/")]), timeout=40)
print(results["result"][0])
driver.quit()
find element
note: this is only experimental yet (not included in pypi package)
from selenium_injector.webdriver import Chrome
from selenium_injector.types.by import By
driver = Chrome(injector_options={"mv2":True, "mv3":True})
driver.get("https://www.wikipedia.org/")
elem = driver.injector.find_elements(By.XPATH, '//*[@id="js-link-box-en"]')
elem[0].click()
driver.quit()
Help
Please feel free to open an issue or fork!
Todo
- eval within tab scope from extension
- mv2
- return
- stringify obj
- mv3
- return
- stringify obj
- mv2
- add events
- make protocoll use
UUIDS's - allow response to event within scope
- using
(...args) => {new event_handler(...args)}
- using
- make protocoll use
- types.eval
- for-loops
- authentificaten proxies
- manage webrtc-leak
- manage location api leak
- proxy per request
- add automation tools
- click
- send_keys
- find_element
- by XPATH
- undetectability
- make tab scripts private
- support base_driver argument
- make
/files/js/utils.jsprivate
Deprecated
Authors
License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Disclaimer
I am not responsible what you use the code for!!! Also no warranty!
Acknowledgments
Inspiration, code snippets, etc.
Metadata
Release files for selenium-injector 2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| selenium_injector-2.3.tar.gz | 27.2 kB | Without provenance | Details for selenium_injector-2.3.tar.gz |
Release files / selenium_injector-2.3.tar.gz
| Download URL | selenium_injector-2.3.tar.gz |
|---|---|
| Size | 27.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6fb8df7da2747189d06d6babf6fcfc5bf88277ad81f5f9f33cb6961b2d4cf362
|
|
BLAKE2b-256 checksum How to use checksums |
793e1a6889bdbd12006bc711a718b9ae0ad10293c8cedee0dd6ad65ff86d37e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.9
|
Security for selenium-injector 2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.