Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more browsers #340

Merged
merged 10 commits into from
Dec 7, 2023
Merged

Support more browsers #340

merged 10 commits into from
Dec 7, 2023

Conversation

jakethekoenig
Copy link
Member

The system default browser is detected and opened if it is of type firefox, safari or edge. Otherwise we fallback on chrome.

The system default browser is detected and opened if it is of type
firefox, safari or edge. Otherwise we fallback on chrome.
@granawkins
Copy link
Member

I got an error testing on Dec 5:

Unhandled Exception: Traceback (most recent call last):
  File "/Users/granthawkins/mentat/mentat/session.py", line 154, in run_main
    await self._main()
  File "/Users/granthawkins/mentat/mentat/session.py", line 115, in _main
    message = await collect_user_input()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/granthawkins/mentat/mentat/session_input.py", line 41, in collect_user_input
    await command.apply(*arguments[1:])
  File "/Users/granthawkins/mentat/mentat/commands.py", line 433, in apply
    image = vision_manager.screenshot(*args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/granthawkins/mentat/mentat/vision/vision_manager.py", line 70, in screenshot
    self.open(path)
  File "/Users/granthawkins/mentat/mentat/vision/vision_manager.py", line 51, in open
    self._open_browser()
  File "/Users/granthawkins/mentat/mentat/vision/vision_manager.py", line 43, in _open_browser
    service = Service(ChromeDriverManager().install())
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/webdriver_manager/chrome.py", line 40, in install
    driver_path = self._get_driver_binary_path(self.driver)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/webdriver_manager/core/manager.py", line 40, in _get_driver_binary_path
    file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/webdriver_manager/drivers/chrome.py", line 32, in get_driver_download_url
    driver_version_to_download = self.get_driver_version_to_download()
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/webdriver_manager/core/driver.py", line 48, in get_driver_version_to_download
    return self.get_latest_release_version()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/webdriver_manager/drivers/chrome.py", line 64, in get_latest_release_version
    determined_browser_version = ".".join(determined_browser_version.split(".")[:3])
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'split'

@jakethekoenig
Copy link
Member Author

It turns out there's no easy cross platform way to detect the users default browser. You actually can't uninstall safari on macs so I just use that on MacOs. On Windows I attempt to use edge then chrome then firefox and on Linux Chrome then firefox. Reporting an error if all fail.

@PCSwingle
Copy link
Member

Hmm, that is annoying; why don't we just fall through all of the browsers regardless of os? So test Safari first, then if that errors try and open firefox, then chrome, etc. and not worry about the os at all.

@jakethekoenig
Copy link
Member Author

Hmm, that is annoying; why don't we just fall through all of the browsers regardless of os? So test Safari first, then if that errors try and open firefox, then chrome, etc. and not worry about the os at all.

Good idea, that is simpler.

self.driver = webdriver.Chrome(service=service)
try:
self.driver = webdriver.Safari()
except Exception:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except Exception:
except Exception as e:
if "remote automation" in str(e).lower():
stream.send(f"To use Safari, enable remote automation.", color="red")

The first time I tried /screenshot, it failed (didn't recognize Safari, no backups installed). I found I needed to:

  1. In the Safari menu bar, click 'Safari' and then 'Preferences'.
  2. Go to the 'Advanced' tab and check 'Show Develop menu in menu bar'.
  3. Open the 'Develop' menu in the menu bar and choose 'Allow Remote Automation'.

..and then it would work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks. I think given that I'll try to open with chrome before telling users to enable remote automation. But if they have safari installed I'll print a different message informing them of that option.

stream.send(
'No browser open. Run "/screenshot path" with a url or local file',
color="red",
"Error taking screenshot. Please run with a valid url or local path.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if this except was inside vision_manager.screenshot; that way we don't have to rewrite this except if we decide to use .screenshot again somewhere.

if safari_installed:
ctx.stream.send(
"No suitable browser found. To use Safari, enable"
" remote automation. Alternatively install Chrome.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
" remote automation. Alternatively install Chrome.",
" remote automation.",

Comment on lines 58 to 59
"No suitable browser found. Install Chrome or"
" Firefox.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"No suitable browser found. Install Chrome or"
" Firefox.",
"No suitable browser found.",

Copy link
Member

@PCSwingle PCSwingle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of really small comments, but it looks good to me! I can't test this but it looks like @granawkins already tested this for Safari so I'm ok approving it.

@jakethekoenig jakethekoenig merged commit cf46c59 into main Dec 7, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants