Multi-threading in selenium python
<p>I am working on a project which needs bit automation and web-scraping for which I am using <strong>Selenium</strong> and <strong>BeautifulSoup (python2.7)</strong>.</p>
<p>I want to open <strong>only one instance of a web browser</strong> and login to a website, <strong>keeping that session</strong>, I am trying to open new tabs which will be independently controlled by threads, <strong>each thread controlling a tab</strong> and performing their own task. How should I do it? An example code would be nice. Well here's my code:</p>
<pre><code>def threadFunc(driver, tabId):
    if tabId == 1:
        #open a new tab and do something in it
    elif tabId == 2:
        #open another new tab with some different link and perform some task
    .... #other cases


class tabThreads(threading.Thread):

    def __init__(self, driver, tabId):
        threading.Thread.__init__(self)
        self.tabID = tabId
        self.driver = driver

    def run(self):
        print &quot;Executing tab &quot;, self.tabID
        threadFunc(self.driver, self.tabID)

def func():
    # Created a main window
    
    driver = webdriver.Firefox()
    driver.get(&quot;...someLink...&quot;)

    # This is the part where i am stuck, whether to create threads and send
    # them the same web-driver to stick with the current session by using the
    # javascript call &quot;window.open('')&quot; or use a separate for each tab to
    # operate on individual pages, but that will open a new browser instance
    # everytime a driver is created

    thread1 = tabThreads(driver, 1)
    thread2 = tabThreads(driver, 2)
    ...... #other threads
</code></pre>
<p>I am <strong>open to suggestions for using any other module</strong>, if needed</p>