Package: selenium-injector
Selenium-InjectorChange proxy while running (auth supported)remotely contoll Chrome using websockets and extensionsFeel free to test my code!Getting StartedDependenciesPython >= 3.7Chrome-BrowserinstalledSeleniumInstalling[Windows] InstallChrome-Browserpip install selenium_injectorExample scriptsset proxy dynamicallyfromselenium_injector.webdriverimportChromedriver=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 executedriver.quit()in the End. Else-wise your temporary folder will get flooded! and it keeps runninguse eventsfromselenium_injector.webdriverimportChromeimportjsondriver=Chrome()driver.get("chrome://version")t=driver.injector.socket.js.typesevent_id=driver.injector.socket.make_event_id()user=driver.injector.any_userdriver.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)foreinevent:# will block forevere=json.loads(e)data=e["result"][0]time=e["t"]print(time+"\n",data['url'])warning: asdriver.quit()isn't called in this example, it will leave files in your temp directoriesmodify network requestsnote: this is only experimental yet (not included in pypi package)example scriptfromselenium_injector.webdriverimportChromedriver=Chrome()# modify headersdriver.injector.declarativeNetRequest.update_headers({"test":"test_2","sec-ch-ua-platform":"Android"})rules=driver.injector.declarativeNetRequest.dynamic_rulesheaders=driver.injector.declarativeNetRequest._headersdriver.get("https://httpbin.org/headers")input("press ENTER to continue")# block imagesdriver.injector.declarativeNetRequest.update_block_on(resource_types=["image"])driver.get("https://www.wikimedia.org/")input("press ENTER to exit")driver.quit()use chrome-developer-protocollnote: this is only experimental yet (not included in pypi package)example scriptimportjsonfromselenium_injector.webdriverimportChromedriver=Chrome()dbg=driver.injector.debuggerdbg.attach()dbg.execute("Console.enable")events=dbg.on_event()driver.execute_script("console.log('Hello World!')")foreventinevents:event=json.loads(event)result=event["result"]time=event["t"]ifresult[1]=='Console.messageAdded':message_text=result[2]["message"]["text"]print(time,message_text)breakdriver.quit()execute script within tabnote: this is only experimental yet (not included in pypi package)from stringfromselenium_injector.webdriverimportChromedriver=Chrome(injector_options={"mv2":True,"mv3":True})driver.get("https://www.wikipedia.org/")# result only returned with mv2 extension enabledresults=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 automaticallyfromselenium_injector.webdriverimportChromedriver=Chrome(injector_options={"mv3":True})driver.get("https://www.wikipedia.org/")t=driver.injector.socket.js.typesresults=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 elementnote: this is only experimental yet (not included in pypi package)fromselenium_injector.webdriverimportChromefromselenium_injector.types.byimportBydriver=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()HelpPlease feel free to open an issue or fork!Todoeval within tab scope from extensionmv2returnstringify objmv3returnstringify objadd eventsmake protocoll useUUIDS'sallow response to event within scopeusing(...args) => {new event_handler(...args)}types.evalfor-loopsauthentificaten proxiesmanage webrtc-leakmanage location api leakproxy per requestadd automation toolsclicksend_keysfind_elementby XPATHundetectabilitymake tab scripts privatesupport base_driver argumentmake/files/js/utils.jsprivateDeprecatedAuthorsAurin AegerterLicenseShield:This work is licensed under aCreative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.DisclaimerI am not responsible what you use the code for!!! Also no warranty!AcknowledgmentsInspiration, code snippets, etc.Selenium-ProfilesChrome-devtools-protocolcdp_event_listenerssync websocket serverchrome-extension-docsPEG-parsermake-SV-stayalivestringify-objinject code from mv3