Well, if I did not misunderstand your scenario, I think the following should work:
In 1.py
:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchAttributeException
from 2 import *
from selenium.webdriver.common.keys import Keys
profile1 = webdriver.FirefoxProfile()
driver = webdriver.Firefox(profile1)
driver.get("https://www.google.com/")
In 2.py
# Importamos selenium porque dices que lo necesitas, pero en el
# ejemplo que pones no haría falta
import selenium
# Y ahora, en vez de crear aquí un nuevo driver, importamos el de 1.py
from 1 import driver
driver.get("https://www.google.es/")
Since the driver
object is created only once, only one tab should be opened.
Still there is a risk of circular imports. Since 1.py
you import all symbols of 2.py
, which forces python to read 2.py
complete at that time. And since 2.py
we make an import of a symbol of 1.py
that is not yet defined. Although I can not prove it, I'm afraid it will cause an irresolvable circularity.
One way to avoid this may be to postpone imports at the time the corresponding symbols are defined, although this may not be possible in your case. The idea could be like this:
1.py
:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchAttributeException
from selenium.webdriver.common.keys import Keys
profile1 = webdriver.FirefoxProfile()
driver = webdriver.Firefox(profile1)
from 2 import *
driver.get("https://www.google.com/")
This circular import is very tricky. It is usually a sign of bad design (why could not everything be in a single module?) And sometimes it works "as if by magic" ... More details here