I am working on an automation script using Selenium and Python. I want to automate some activities on Instagram and check some IP-related info. The automation script needs to run on the Opera web browser.
Initially, I was running my script in a single file without functions and classes and it worked fine. However, when I tried to split it into functions and classes for better code structure and readability, it started crashing.
The error messages I'm seeing are as follows:
[ERROR] Error opening link: HTTPConnectionPool(host='localhost', port=65032): Max retries exceeded with url: /session/252d00b6bf664f61d5fb0d6452bff83f/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001E872646010>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
I also noticed some console errors, but I'm not sure if they are related:
[ERROR:CONSOLE(0)] "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist."
Here's how I have it in the all-in-one version where it doesn't crash:
webdriver_service = service.Service(CONFIG['web_driver']['driver_path'])
webdriver_service.start()
options = webdriver.ChromeOptions()
options.binary_location = CONFIG['web_driver']['binary_location']
options.add_experimental_option('w3c', True)
options.add_extension("./3.4.8_0.crx")
options.add_argument(f"user-data-dir={CONFIG['web_driver']['user_data_path']}")
options.add_argument("--start-maximized")
driver = webdriver.Remote(webdriver_service.service_url, options=options)
driver.get('https://www.google.com/')
In the other version, the code is divided into two main parts: app.py where the main logic resides, and selenium_manager.py where I have abstracted out the Selenium-related functions.
app.py:
class MainApp:
def __init__(self):
self.selenium_manager = SeleniumManager(logger, CONFIG["web_driver"])
self.driver = self.selenium_manager.open_link("https://www.google.com/")
def check_ip(self):
...
if __name__ == "__main__":
main_app = MainApp()
time.sleep(30)
main_app.check_ip()
time.sleep(30)
main_app.selenium_manager.quit()
and selenium_manager.py:
class SeleniumManager:
def __init__(self, logger, web_driver_settings):
self.logger = logger
self.logger.info("Initializing Selenium driver...")
webdriver_service = service.Service(web_driver_settings["driver_path"])
webdriver_service.start()
options = webdriver.ChromeOptions()
options.binary_location = web_driver_settings["binary_location"]
options.add_experimental_option('w3c', True)
options.add_extension("...path-to-extension")
options.add_argument(f"user-data-dir={web_driver_settings['user_data_path']}")
options.add_argument('--disable-blink-features=AutomationControlled')
options.headless = web_driver_settings["headless"]
self.driver = webdriver.Remote(webdriver_service.service_url, options=options)
self.driver.maximize_window()
self.wait = WebDriverWait(self.driver, web_driver_settings["wait_time"])
self.logger.info("Selenium driver initialized successfully!")
def quit(self):
self.driver.quit()
self.logger.info("Selenium driver closed successfully!")
def open_link(self, href):
try:
self.driver.get(href)
except Exception as e:
self.logger.error(f"Error opening link: {e}")
.....
- What could be causing this error and how do I fix it?
- Is it something related to the fact that I'm using Opera?
- Should I consider using another browser? Is there another automation framework that works well with Opera?