Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:33:33+00:00 2026-06-18T11:33:33+00:00

How would you create an instance of a webdriver the first time the script

  • 0

How would you create an instance of a webdriver the first time the script is called, and then retrieve that same instance the next times? Something like this pseudocode:

from selenium import webdriver

thisScript = FIXME

if thisScript.isRunning():
    driver = thisScript.driver

else:        
    driver = webdriver.Firefox()

driver.get("http://www.example.com")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:33:34+00:00Added an answer on June 18, 2026 at 11:33 am

    The best option seems to be setting up a server/client system, I guess it could be done using the selenium server, but I wound up coding my own, something like this:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # This script takes a html file name as argument to start the
    # selenium webdriver. It will start a server the first time it's run.
    # If called again, it will then check if the server is running
    # and restart it if neccessary. This prevents from having to restart
    # the browser when running tests from different scripts by facilitating
    # the running instance of selenium.
    
    import socket, threading, time, commands, os
    
    from selenium import webdriver
    
    dirPath = "/path/to/dir"
    fileUri = "/path/to/file"
    
    class ThreadServer(threading.Thread):
        def __init__(self, port=None, host=None):
            threading.Thread.__init__(self)
    
            self.host = host if host != None else "localhost"
            self.port = port if port != None else self.getFreePort()
    
            self.driver = webdriver.Firefox()
    
        def getFreePort(self):
            sock = socket.socket()
            sock.bind(('', 0))
    
            port = sock.getsockname()[1]
    
            sock.close()
    
            return port
    
        def run(self):
            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server.bind((self.host, self.port))
    
            port = server.getsockname()[1]
            print 'Server started successfully {0}'.format(port)
    
            server.listen(1)
            conn, addr = server.accept()
    
            while 1:
                try:
                    fileUrl = conn.recv(4096)
    
                except socket.error:
                    server.listen(1)
                    conn, addr = server.accept()
                    continue
    
                if not fileUrl:
                    server.listen(1)
                    conn, addr = server.accept()
    
                else:
                    self.driver.get(fileUrl)
    
    host = '127.0.0.1'
    port = 44164
    
    if dirPath == "/path/to/dir":
        fileUrl = 'http://localhost/{0}'.format(fileUri.lstrip(dirPath))
    
        try:
            sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            sock.settimeout(3.0)
            sock.connect((host, port))
    
        except socket.error:
            threadServer = ThreadServer(port)
            threadServer.start()
    
            time.sleep(3)
    
            sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            sock.settimeout(3.0)
            sock.connect((host, port))
    
        try:
            sock.send(fileUrl)
    
        except socket.error:
            raise
    
        else:
            sock.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to create a variable that holds an instance
I need to have script.sh , that would create files f1.txt and f2.txt with
As the title says. How would I create an instance of a class that
What would be correct way to create new instance of the class from class
Versioning If your events changes you would create a new version of that event,
How would I create a Mongoid model that has the ability to be saved
How would I create a DecimalFormat that would only have seven digits total and
Copying an array would create a new instance of the array, but the objects
I would like to create an instance of a specified class using its name.
Is there any way to create all instance properties dynamically? For example, I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.