1

Exactly what the title says. When ran, Chrome does in fact open, but does not continue on because unable to connect. Prints

opening chrome (mac)
opened chrome
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:9515
from chrome not reachable
Stacktrace:

When I run chromedriver in terminal it successfully starts and returns

Starting ChromeDriver 110.0.5481.77 () on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

Current version of Chrome is 110.0.5481.100

Code is as follows

def open_chrome(port=9515, on_mac=True):
    my_env = os.environ.copy()
    if on_mac:
        print('opening chrome (Mac)')
        subprocess.Popen(['open', '-a', "Google Chrome", "--args",
                          f'--remote-debugging-port={port}', 'http://www.example.com'])  # , env=venv)
    else:
        print('opening chrome (Linux)')
        subprocess.Popen(
            f'google-chrome --remote-debugging-port={port} --user-data-dir=data_dir'.split(), env=venv)
    print('opened chrome')

class Bot():
    def __init__(self, port_no=9515, headless=False, verbose=False):
        print('initialising bot')

        options = Options()
        if headless:
            options.add_arguments("--headless")
        else:
            open_chrome()
            # attatch to the same port that you're running chrome on
            options.add_experimental_option(
                f"debuggerAddress", f"127.0.0.1:{port_no}")
        # without this, the chrome webdriver can't start (SECURITY RISK)
        options.add_argument("--no-sandbox")
        # options.add_arguement("--window-size=1920x1080")
        self.driver = webdriver.Chrome(options=options)
        self.verbose = verbose

echo $PATH returns /Library/Frameworks/Python.framework/Versions/3.10/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Most of the stuff I can find on similar problems suggest PATH issue but because running chromedriver is successful I don't think it's PATH problem? But also not sure since I don't see it in $PATH...

creetz
  • 21
  • 3
  • Does it work if you use something like this answer instead? https://stackoverflow.com/questions/39428042/use-selenium-with-chromedriver-on-mac – Jortega Feb 25 '23 at 22:41
  • @Jortega yes, I ran that exact code and it opened chrome and went to baidu.com – creetz Feb 25 '23 at 23:02
  • Is there a reason you need chrome to use 127.0.0.1:9515? Or do you not care and just want the browser to open and work? – Jortega Feb 25 '23 at 23:24
  • @Jortega, you had the right idea. i just removed all the port statements and it runs. now just having a problem with the parent script to this script looping in the middle of the instead of running to the end, but thats an entirely different problem. thank you tho! – creetz Feb 25 '23 at 23:59

0 Answers0