Browse Source

Ok, I think this is everything. Not sure.

Steve Thielemann 3 weeks ago
parent
commit
b197aa6ae2
2 changed files with 34 additions and 0 deletions
  1. 33 0
      browser.py
  2. 1 0
      useragent.py

+ 33 - 0
browser.py

@@ -0,0 +1,33 @@
+#
+# Browser control
+#
+# This provides a headless google chrome image with correct useragent
+#
+
+from selenium import webdriver
+from useragent import USERAGENT
+
+WIDTH = 1280
+HEIGHT = 1024
+
+# USERAGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
+
+
+def get_driver(url=None):
+    options = webdriver.ChromeOptions()
+    options.add_argument("--no-sandbox")
+    options.add_argument("--disable-gpu")
+    options.add_argument("--disable-dev-shm-usage")
+    # We must correct the user-agent because headless.
+    options.add_argument(f"user-agent={USERAGENT}")
+    options.add_argument(f"window-size={WIDTH},{HEIGHT}")
+    # Headless changes UserAgent from Chrome to HeadlessChrome.
+    options.add_argument("--headless=new")
+    if url is None:
+        url = "http://127.0.0.1:9515"
+    driver = webdriver.Remote(command_executor=url, options=options)
+    return driver
+
+
+def get_useragent(driver):
+    return driver.execute_script("return navigator.userAgent;")

+ 1 - 0
useragent.py

@@ -0,0 +1 @@
+USERAGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"