DedInc / pyanty

Python module for controlling Dolphin browser profiles using Selenium, Pyppeteer, and Playwright. Includes Dolphin API for profile management.
https://pypi.org/project/pyanty
MIT License
45 stars 14 forks source link

Crash on a simple example #3

Closed JohnScience closed 8 months ago

JohnScience commented 8 months ago

Script:

import selenium_dolphin as dolphin
# from selenium.webdriver.chrome.options import Options

api_key = 'My (JohnScience) API key'
api = dolphin.DolphinAPI(api_key)
profiles = api.get_profiles()['data']
# print(profiles)
profile_id = profiles[0]['id']

response = dolphin.run_profile(profile_id)
print(response)
port = response['automation']['port']
print(port)
driver = dolphin.get_driver(port=port)
driver.get('https://google.com/')
print(driver.title)
driver.quit()

dolphin.close_profile(profile_id)

Output:

C:\Users\USER\Documents\github\dolphin-test>python main.py
{'success': True, 'automation': {'port': 62423, 'wsEndpoint': '/devtools/browser/9a161da6-64a6-4bb4-b9f0-c494b85b9a52'}}
62423
Traceback (most recent call last):
  File "C:\Users\USER\Documents\github\dolphin-test\main.py", line 18, in <module>
    driver = dolphin.get_driver(port=port)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\selenium_dolphin\selenium_dolphin.py", line 71, in get_driver
    driver_path = get_dolphin_driver()
                  ^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\selenium_dolphin\selenium_dolphin.py", line 66, in get_dolphin_driver
    return driver_path
           ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'driver_path' where it is not associated with a value
JohnScience commented 8 months ago

Source of the problem:

selenium_dolphin/selenium_dolphin.py:

def get_dolphin_driver():
    html = requests.get('https://dolphin-anty.com/docs/basic-automation/#selenium').text
    driver_slices = html.split('/chromedriver')
    server_url = driver_slices[0].split('"')[-1]
    version = driver_slices[1].split('"')[0]
    file_name = 'chromedriver' + version
    driver_url = server_url + '/' + file_name

    if not os.path.exists(file_name) and not os.path.exists('chromedriver'):
        download_driver(driver_url, file_name)
        with zipfile.ZipFile(file_name, 'r') as z:
            z.extractall('')

    if os.path.exists('chromedriver'):
        drivers = os.listdir('chromedriver')
        system = platform.system()
        architecture = platform.machine()
        for driver_file in drivers:         
            if system == 'Windows' and '.exe' in driver_file:
                if '64' in architecture and '64' in driver_file:
                    break
                elif '32' in driver_file and not '64' in architecture:
                    break
            elif system == 'Darwin':
                if 'arm' in architecture and 'arm' in driver_file:
                    break
                elif 'intel' in driver_file:
                    break
            elif system == 'Linux' and 'linux' in driver_file:
                break
        if not driver_file:
            raise ValueError("Unsupported platform")

        driver_path = os.path.join(os.getcwd(), 'chromedriver', driver_file)
    return driver_path

More precisely,

def get_dolphin_driver():
    # ...
    if os.path.exists('chromedriver'):
        # ...
        driver_path = os.path.join(os.getcwd(), 'chromedriver', driver_file)
    return driver_path

I.e. driver_path gets defined only in one of the branches of the if statement.

JohnScience commented 8 months ago

@DedInc Fixed the problem in #4