Skip to content

Commit 634b696

Browse files
authored
Merge pull request #14 from rapatel0/remoteDriver
Tweaks to add remote selenium driver
2 parents 18ea102 + 19ff6e6 commit 634b696

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

browserpilot/agents/gpt_selenium_agent.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .compilers.instruction_compiler import InstructionCompiler
1919
from .memories import Memory
2020

21+
2122
TIME_BETWEEN_ACTIONS = 0.01
2223

2324
import logging
@@ -55,6 +56,7 @@ def __init__(
5556
debug_html_folder="",
5657
instruction_output_file=None,
5758
close_after_completion=True,
59+
remote_url=None,
5860
):
5961
"""Initialize the agent.
6062
@@ -89,8 +91,8 @@ def __init__(
8991
or instruction_output_file.endswith(".json")
9092
), "Instruction output file must be a YAML or JSON file or None."
9193
assert (
92-
chromedriver_path is not None
93-
), "Please provide a path to the chromedriver executable."
94+
(chromedriver_path is not None) ^ (remote_url is not None) # XOR
95+
), "Please provide a path to the chromedriver executable or Selenium Grid target"
9496
self.model_for_instructions = model_for_instructions
9597
self.model_for_responses = model_for_responses
9698
logger.info(f"Using model for instructions: {model_for_instructions}")
@@ -101,6 +103,7 @@ def __init__(
101103
self.debug_html_folder = debug_html_folder
102104
self.memory_folder = memory_folder
103105
self.close_after_completion = close_after_completion
106+
self.remote_url = remote_url
104107

105108
"""Fire up the compiler."""
106109
self.instruction_compiler = InstructionCompiler(
@@ -116,26 +119,31 @@ def __init__(
116119

117120
"""Set up the driver."""
118121
_chrome_options = webdriver.ChromeOptions()
119-
_chrome_options.add_argument(f"user-data-dir={user_data_dir}")
120122
# 🤫 Evade detection.
121123
# https://stackoverflow.com/questions/53039551/selenium-webdriver-modifying-navigator-webdriver-flag-to-prevent-selenium-detec
122124
_chrome_options.add_argument('--disable-blink-features=AutomationControlled')
123125
_chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
124126
_chrome_options.add_experimental_option('useAutomationExtension', False)
125-
127+
_chrome_options.add_argument(f"user-data-dir={user_data_dir}")
126128
self.headless = headless
127129
if headless:
128130
_chrome_options.add_argument("--headless")
129131
for option in chrome_options:
130-
_chrome_options.add_argument(f"{option}={chrome_options[option]}")
131-
132-
# Instantiate Service with the path to the chromedriver and the options.
133-
service = Service(chromedriver_path)
134-
self.driver = webdriver.Chrome(service=service, options=_chrome_options)
132+
if chrome_options[option] == None:
133+
_chrome_options.add_argument(f"{option}")
134+
else:
135+
_chrome_options.add_argument(f"{option}={chrome_options[option]}")
136+
137+
# Check if remote_url is set and conditionally set the driver to a remote endpoint
138+
if remote_url:
139+
self.driver = webdriver.Remote(command_executor=remote_url, options=_chrome_options)
140+
else:
141+
# Instantiate Service with the path to the chromedriver and the options.
142+
service = Service(chromedriver_path)
143+
self.driver = webdriver.Chrome(service=service, options=_chrome_options )
135144
# 🤫 Evade detection.
136145
self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
137146

138-
139147
"""Helper functions"""
140148

141149
def _check_danger(self, action_str):

0 commit comments

Comments
 (0)