add headless mode
This commit is contained in:
parent
577ec26307
commit
7cc9b6c7a3
1 changed files with 12 additions and 4 deletions
16
bidi.py
16
bidi.py
|
|
@ -4,6 +4,7 @@ import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
@ -37,15 +38,20 @@ class LogMessage:
|
||||||
|
|
||||||
|
|
||||||
class WebdriverBidi:
|
class WebdriverBidi:
|
||||||
def __init__(self, browser) -> None:
|
def __init__(self, browser, headless=False) -> None:
|
||||||
|
self.headless = headless
|
||||||
|
|
||||||
# TODO: make dynamic
|
# TODO: make dynamic
|
||||||
self.webdriver_port = 12345
|
self.webdriver_port = 12345
|
||||||
self.webdriver_url = f"http://localhost:{self.webdriver_port}"
|
self.webdriver_url = f"http://localhost:{self.webdriver_port}"
|
||||||
|
|
||||||
|
chrome_binary = "/usr/lib64/chromium-browser/headless_shell" if self.headless else "/usr/bin/chromium-browser"
|
||||||
|
|
||||||
session_args = {"capabilities": {
|
session_args = {"capabilities": {
|
||||||
"alwaysMatch": {
|
"alwaysMatch": {
|
||||||
"webSocketUrl": True,
|
"webSocketUrl": True,
|
||||||
"goog:chromeOptions": {"binary": "/usr/bin/chromium-browser"},
|
"goog:chromeOptions": {"binary": chrome_binary},
|
||||||
|
"moz:firefoxOptions": {"args": ["-headless"] if self.headless else []}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
@ -129,7 +135,8 @@ class WebdriverBidi:
|
||||||
awaitPromise=False, target={"context": context})
|
awaitPromise=False, target={"context": context})
|
||||||
await self.command("browsingContext.navigate", context=context, url="https://piware.de")
|
await self.command("browsingContext.navigate", context=context, url="https://piware.de")
|
||||||
|
|
||||||
await asyncio.sleep(5)
|
if not self.headless:
|
||||||
|
await asyncio.sleep(3)
|
||||||
self.task_reader.cancel()
|
self.task_reader.cancel()
|
||||||
del self.task_reader
|
del self.task_reader
|
||||||
|
|
||||||
|
|
@ -147,5 +154,6 @@ class WebdriverBidi:
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
d = WebdriverBidi("chromium") # or firefox
|
d = WebdriverBidi(sys.argv[1] if len(sys.argv) > 1 else 'chromium',
|
||||||
|
headless=True if len(sys.argv) > 2 and sys.argv[2] == 'headless' else False)
|
||||||
asyncio.run(d.run())
|
asyncio.run(d.run())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue