-2

As a second question, if I have two separate selenium scripts running at the same time, can they interfere with each other, or do they create separate instances?

I'm actually using these to log into my bank. I'm concerned that if I am half way into the login (three separate pages) and then the second script tries to start it from the beginning things could get messy.

As per a question I've included my function:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import time
import json
import traceback

def scrapeBank(bank, return_dict):

    try:

        remote = 1
        debug = 0

        if remote == 1:
            from pyvirtualdisplay import Display
            display = Display(visible=0, size=(800, 600))
            display.start()
            options = webdriver.ChromeOptions()
            options.add_argument('--no-sandbox')
            options.add_argument('--disable-extensions')
            options.add_argument('--headless')
            options.add_argument('--disable-gpu')
            driver = webdriver.Chrome(chrome_options=options)
        else:
            driver = webdriver.Chrome()

        [do a bunch of stuff]

        print('Bank Scrape completed')  
        driver.close()
        return_dict['transactions'] = transactions


    except:
        print(traceback.format_exc())
        driver.close()
mcplums
  • 159
  • 2
  • 2
  • 9

1 Answers1

0

As long as you create a new instance / object, you can run it even in the same script ( you dont need to run the script again )

If you start yout script multiple times, python will create a new object everytime you run it, so you cant control the other driver

Here is how you can create 1 object:

driver = webdriver.Chrome('/path/to/chromedriver')  

multiple would look like this:

driver1 = webdriver.Chrome('/path/to/chromedriver')  
driver2 = webdriver.Chrome('/path/to/chromedriver')  
driver3 = webdriver.Chrome('/path/to/chromedriver')  
driver4 = webdriver.Chrome('/path/to/chromedriver')  

then you can use them like this:

driver1.get('http://www.google.com')
driver2.get('http://www.youtube.com')
driver3.get('http://www.facebook.com')
driver4.get('http://www.stackoverflow.com')

To your code, you do not have a basic knowladge about keywords and how do they acctually work. try means that its going to do the code and if there is an error it will do whats in except. Then there you have a useless condition: if remote == 1: this is always true, cuz 2 lines above is : remote = 1 ... anyways your question is a way different ... You are asking if when you use this function, if browsers are gonna interrupt each other... the answer is NO, as I explained above, everytime you make a new instance / object, its a completely new driver. The reason why is here: driver = webdriver.Chrome(chrome_options=options)

try:
    remote = 1
    debug = 0
    if remote == 1:
        from pyvirtualdisplay import Display
        display = Display(visible=0, size=(800, 600))
        display.start()
        options = webdriver.ChromeOptions()
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-extensions')
        options.add_argument('--headless')
        options.add_argument('--disable-gpu')
        driver = webdriver.Chrome(chrome_options=options)
    else:
        driver = webdriver.Chrome()

    [do a bunch of stuff]

    print('Bank Scrape completed')  
    driver.close()
    return_dict['transactions'] = transactions
except:
    print(traceback.format_exc())
    driver.close()
StyleZ
  • 1,276
  • 3
  • 11
  • 27
  • My situation is that I have a single function which initiates the driver, but this function is called by a variety of different scripts. I think what you are saying is, even if I only have one driver per the function- no driver2 or driver3- a new object will be opened each time the function is called? If so, how can I keep track of, and close, all my open drivers in situations where the script fails before it hits driver.close() – mcplums Mar 06 '19 at 17:13
  • Ok thanks. Sorry if dumb question but what part of the code do you want to see? Can't copy it all :) – mcplums Mar 06 '19 at 18:22
  • The function you want to call from all programs – StyleZ Mar 06 '19 at 18:35
  • I have added my function to my original question. – mcplums Mar 07 '19 at 17:32
  • Thanks for your answer. I don't really understand your issue re. the try/catch statement and redundant variable (I know it is, it helps me test!) but no matter. I understand your point about different drivers. And this brings me to my current problem which I just made a new post about, I'd appreciate it if you could take a look https://stackoverflow.com/questions/55077736/selenium-python-web-scraper-clogging-my-ram-drivers-and-xvfb-display-not-closin – mcplums Mar 09 '19 at 13:18