Program runs in sublime text editor but not in terminal

32 views Asked by At

I have a problem. I wrote a program that harvests emails from a website using selenium in python. I wrote it inside the sublime text editor. Here is my code so far:

from selenium import webdriver
from selenium.webdriver import FirefoxOptions

class colors:
    RESET = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    BLACK = '\033[30m'
    RED = '\033[31m'
    GREEN = '\033[32m'
    YELLOW = '\033[33m'
    BLUE = '\033[34m'
    MAGENTA = '\033[35m'
    CYAN = '\033[36m'
    WHITE = '\033[37m'
    BOLDBLACK = '\033[1;30m'
    BOLDRED = '\033[1;31m'
    BOLDGREEN = '\033[1;32m'
    BOLDYELLOW = '\033[1;33m'
    BOLDBLUE = '\033[1;34m'
    BOLDMAGENTA = '\033[1;35m'
    BOLDCYAN = '\033[1;36m'
    BOLDWHITE = '\033[1;37m'

class App:
    def __init__(self):
        self.emails = []
        self.opts = FirefoxOptions()
        self.opts.add_argument("--headless")
        self.opts.binary_location = "/usr/bin/firefox"
        self.driver = webdriver.Firefox(options=self.opts)
        self.read_urls()
        self.driver.close()
    def read_urls(self):
        urls = [ "https://wertweoritwoertiwoerit.com/", "https://rwteirtwertwertwertwe.com/" ]
        for url in urls:
            self.scan(url)
        #print(self.emails)
    def scan(self,url):
        self.driver.get(url)
        source_code = self.driver.page_source
        body = self.driver.find_element("css selector", "body")
        visible_text = body.text
        edited_visible_text = visible_text.replace("\n", " ")
        array = edited_visible_text.split(' ')
        for string in array:
            if "@" in string:
                self.emails.append(string)
                print(""+colors.BOLDBLUE + f"[+]{string}"+colors.RESET)
if __name__ == "__main__":
    main = App()

I just started that, i know the code looks awful and is poorly written, but that doesn't matter right now!

Important is: When i run Ctrl + B, OR (same functionality) press Ctrl + P and select "Build with python", then it executes and runs successfully...

The error shows up after running the following line:

driver = webdriver.Firefox()

When i then run it inside any terminal, i get this error:

╭─root at kali in ~kali 24-03-24 - 18:58:52
╰─○ python3 harvest.py
Traceback (most recent call last):
  File "/home/kali/harvest.py", line 54, in <module>
    main = App()
           ^^^^^
  File "/home/kali/harvest.py", line 34, in __init__
    self.driver = webdriver.Firefox(options=self.opts)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/firefox/webdriver.py", line 69, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
    self.start_session(capabilities)
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

The sublime-text build option uses this file (without this file it doesn't work): /root/.config/sublime-text/Lib/python33/package_control.py

Which contains:

import os
import sys
import zipfile

import sublime_plugin

__data_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

__pkg_path = os.path.join(__data_path, 'Packages', 'Package Control', 'package_control')
__zip_path = os.path.join(__data_path, 'Installed Packages', 'Package Control.sublime-package')

__code = None

# We check the .sublime-package first, since the sublime_plugin.ZipLoader deals with overrides
if os.path.exists(__zip_path):
    __pkg_path = os.path.join(__zip_path, 'package_control')
    __file_path = os.path.join(__pkg_path, '__init__.py')

    __loader__ = sublime_plugin.ZipLoader(__zip_path)

    try:
        with zipfile.ZipFile(__zip_path, 'r') as __f:
            __code = compile(
                __f.read('package_control/__init__.py').decode('utf-8'),
                '__init__.py',
                'exec'
            )
    except (OSError, KeyError):
        pass

    # may be required before Package Control has been loaded
    events = sys.modules.get('package_control.events')
    if events is None:
        events = __loader__.load_module("Package Control.package_control.events")
        events.__name__ = 'package_control.events'
        events.__package__ = 'package_control'
        sys.modules['package_control.events'] = events

elif os.path.exists(__pkg_path):
    from importlib.machinery import SourceFileLoader

    __file_path = os.path.join(__pkg_path, '__init__.py')

    __loader__ = SourceFileLoader('package_control', __file_path)

    try:
        with open(__file_path, 'r', encoding='utf-8') as __f:
            __code = compile(__f.read(), '__init__.py', 'exec')
    except (OSError):
        pass

    # may be required before Package Control has been loaded
    events = sys.modules.get('package_control.events')
    if events is None:
        events = SourceFileLoader('events', os.path.join(__pkg_path, 'events.py')).load_module()
        events.__name__ = 'package_control.events'
        events.__package__ = 'package_control'
        sys.modules['package_control.events'] = events

    del globals()['SourceFileLoader']

if __code is None:
    raise ModuleNotFoundError("No module named 'package_control'")

__file__ = __file_path
__package__ = 'package_control'
__path__ = [__pkg_path]

# initial cleanup
del globals()['__f']
del globals()['__file_path']
del globals()['__zip_path']
del globals()['__pkg_path']
del globals()['__data_path']
del globals()['sublime_plugin']
del globals()['zipfile']
del globals()['sys']
del globals()['os']

__data = {}
exec(__code, __data)
globals().update(__data)

# Python 3.3 doesn't have __spec__
if hasattr(globals(), '__spec__'):
    __spec__.loader = __loader__
    __spec__.origin = __file__
    __spec__.submodule_search_locations = __path__
    __spec__.cached = None

# final cleanup
del globals()['__data']
del globals()['__code']
# out-dated internals
del globals()['__cached__']

Summary:

The code runs in subl but not in the normal terminal.

I've done a lot of research. I read that geckodriver outpust a log file, but it didn't. I don't know how to actually debug this, because i already set the paths for the firefox binary and the geckodriver driver. I added options as you can see and it still doesn't work.

Does anyone have a clue whats going on here? Thank you in advance.

0

There are 0 answers