Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions botcity/web/browsers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.service import Service as ChromeService # noqa: F401, F403

from ..util import is_admin


def default_options(headless=False, download_folder_path=None, user_data_dir=None,
page_load_strategy="normal", binary_path: str = None) -> ChromeOptions:
Expand Down Expand Up @@ -52,9 +54,16 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non

chrome_options.add_argument("--disable-blink-features=AutomationControlled")

# Disable banner for Browser being remote-controlled
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)

# Check if user is root
if is_admin():
if os.name == "posix":
chrome_options.add_argument("--no-sandbox")
else:
# Disable banner for Browser being remote-controlled
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--headless=new")
Expand All @@ -63,13 +72,6 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
chrome_options.add_argument("--hide-scrollbars")
chrome_options.add_argument("--mute-audio")

# Check if user is root
try:
# This is only valid with Unix
if os.geteuid() == 0:
chrome_options.add_argument("--no-sandbox")
except AttributeError:
pass
chrome_options._botcity_temp_dir = None
if not user_data_dir:
temp_dir = tempfile.TemporaryDirectory(prefix="botcity_")
Expand Down
20 changes: 10 additions & 10 deletions botcity/web/browsers/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from selenium.webdriver import Edge, EdgeOptions # noqa: F401, F403
from selenium.webdriver.edge.service import Service as EdgeService # noqa: F401, F403

from ..util import is_admin


def default_options(headless=False, download_folder_path=None, user_data_dir=None,
page_load_strategy="normal", binary_path: str = None) -> EdgeOptions:
Expand Down Expand Up @@ -48,10 +50,16 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
edge_options.add_argument("--disable-features=msSmartScreenProtection")
edge_options.add_argument("--disable-blink-features=AutomationControlled")

# Disable banner for Browser being remote-controlled
edge_options.add_experimental_option("excludeSwitches", ["enable-automation"])
edge_options.add_experimental_option('useAutomationExtension', False)

# Check if user is root
if is_admin():
if os.name == "posix":
edge_options.add_argument("--no-sandbox")
else:
# Disable banner for Browser being remote-controlled
edge_options.add_experimental_option("excludeSwitches", ["enable-automation"])

if headless:
edge_options.add_argument("--headless")
edge_options.add_argument("--headless=new")
Expand All @@ -60,14 +68,6 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
edge_options.add_argument("--hide-scrollbars")
edge_options.add_argument("--mute-audio")

# Check if user is root
try:
# This is only valid with Unix
if os.geteuid() == 0:
edge_options.add_argument("--no-sandbox")
except AttributeError:
pass

edge_options._botcity_temp_dir = None
if not user_data_dir:
temp_dir = tempfile.TemporaryDirectory(prefix="botcity_")
Expand Down
17 changes: 17 additions & 0 deletions botcity/web/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import shutil
import tempfile

Expand All @@ -19,6 +20,22 @@ def cleanup_temp_dir(temp_dir: tempfile.TemporaryDirectory) -> None:
shutil.rmtree(temp_dir.name, ignore_errors=True)


def is_admin():
if os.name == "nt":
# Only applies to Windows
import ctypes
try:
return ctypes.windll.shell32.IsUserAnAdmin() != 0
except Exception:
return False
else:
# Unix/Linux check
try:
return os.geteuid() == 0
except Exception:
return False


def element_as_select(element: WebElement) -> Select:
"""Wraps a WebElement in a Select object.
Expand Down