如何在不同的 python 文件中使用相同的 selenium web 驱动程序实例?
How to use same instance of selenium web driver across different python files?
我需要在不同的 python 文件中使用相同的 selenium 网络驱动程序实例。这是我的基地 class.
class Automate(object):
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.add_argument('--user-data-dir=\profile path')
self.driver = webdriver.Chrome(executable_path="\driver path", options=self.options)
def get(self, url):
self.driver.get(url)
现在,当我尝试在 temp.py
和 test.py
等两个不同的文件中为此 class 实例化一个对象时,
在temp.py
import automate
driver1 = Automate()
driver1.get("google.com")
在test.py
import automate
driver2 = Automate()
driver2.get("google.com")
结果是打开两个单独的 chrome windows。
我希望这两个文件只使用网络驱动程序的一个实例。我试图搜索答案,有人说使用单例classes(我对单例了解不多class)。
谁能给我一个关于如何跨多个 python 文件使用单个驱动程序实例的示例。
我想要的:
我需要第一个文件来打开浏览器和第二个文件来发送命令,如 get,通过 xpath 查找元素。另外,我希望第二个文件可以在不打开新浏览器的情况下重新运行window。
我通过使用 chrome 远程调试找到了解决问题的方法。首先,我用这段代码检查了 chrome 是否是 运行 。我知道这很少,但无论如何,它对我有用。我创建了几个文件 singleton.py
、admin_chrome.bat
、chrome.bat
在admin_chrome.bat
@echo off
start "" Powershell -Command "& { Start-process \"chrome.bat\" -ArgumentList @(\"C:\Windows\System32\drivers\etc\hosts\") -Verb RunAs } "
在chrome.bat
,
@echo off
start "" chrome.exe --remote-debugging-port=9222 --user-data-
dir="F:\Programs\Projects Folder\ChromeProfile\Default"
exit
在singleton.py
import asyncio
import psutil
import os
admin = r'admin_chrome.bat'
chrome_status = "temp.txt"
async def initialize():
if not await chrome_status_checker():
await chrome_status_setter(True)
os.system(admin)
print("chrome is opening, please wait...")
await asyncio.sleep(15)
else:
print("chrome already opened")
return False
return True
async def chrome_status_setter(status):
test = open(chrome_status, "w+")
if status:
process_name = "chrome.exe"
for proc in psutil.process_iter():
if proc.name() == process_name:
test.write(str(proc) + "\n")
test.close()
async def chrome_status_checker():
test = open(chrome_status, "r+")
status = test.read()
process_name = "chrome.exe"
true = []
false = []
for proc in psutil.process_iter():
if proc.name() == process_name:
if str(proc) in status:
true.append(str(proc))
else:
false.append(str(proc))
if len(true) > len(false):
return True
else:
return False
在Automate.py
class Automate(object):
def __init__(self):
self.options = webdriver.ChromeOptions()
loop_main = asyncio.new_event_loop()
loop_main.run_until_complete(singleton.initialize())
loop_main.close()
self.options.add_argument('--user-data-dir=F:\profile path')
self.options.add_experimental_option('debuggerAddress', 'localhost:9222')
self.driver = webdriver.Chrome(executable_path="F:\driver path", options = self.options)
这对我有用。如果有更好更简单的解决方案,请告诉我。
对我有用的更简单的解决方案(至少到目前为止)。 Python 允许您轻松导入 - 如果您正在下降 directories/packages。我正在使用包。我创建了一个简单的驱动程序文件,我在其中引用了驱动程序路径。然后我将该文件导入到位于 'one step up in directory.'
的包中
在我想重新使用驱动程序的包中,我调用“from 'package.name' import 'driver'
然后我可以在我的脚本中使用驱动程序。
我需要在不同的 python 文件中使用相同的 selenium 网络驱动程序实例。这是我的基地 class.
class Automate(object):
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.add_argument('--user-data-dir=\profile path')
self.driver = webdriver.Chrome(executable_path="\driver path", options=self.options)
def get(self, url):
self.driver.get(url)
现在,当我尝试在 temp.py
和 test.py
等两个不同的文件中为此 class 实例化一个对象时,
在temp.py
import automate
driver1 = Automate()
driver1.get("google.com")
在test.py
import automate
driver2 = Automate()
driver2.get("google.com")
结果是打开两个单独的 chrome windows。 我希望这两个文件只使用网络驱动程序的一个实例。我试图搜索答案,有人说使用单例classes(我对单例了解不多class)。
谁能给我一个关于如何跨多个 python 文件使用单个驱动程序实例的示例。
我想要的: 我需要第一个文件来打开浏览器和第二个文件来发送命令,如 get,通过 xpath 查找元素。另外,我希望第二个文件可以在不打开新浏览器的情况下重新运行window。
我通过使用 chrome 远程调试找到了解决问题的方法。首先,我用这段代码检查了 chrome 是否是 运行 。我知道这很少,但无论如何,它对我有用。我创建了几个文件 singleton.py
、admin_chrome.bat
、chrome.bat
在admin_chrome.bat
@echo off
start "" Powershell -Command "& { Start-process \"chrome.bat\" -ArgumentList @(\"C:\Windows\System32\drivers\etc\hosts\") -Verb RunAs } "
在chrome.bat
,
@echo off
start "" chrome.exe --remote-debugging-port=9222 --user-data-
dir="F:\Programs\Projects Folder\ChromeProfile\Default"
exit
在singleton.py
import asyncio
import psutil
import os
admin = r'admin_chrome.bat'
chrome_status = "temp.txt"
async def initialize():
if not await chrome_status_checker():
await chrome_status_setter(True)
os.system(admin)
print("chrome is opening, please wait...")
await asyncio.sleep(15)
else:
print("chrome already opened")
return False
return True
async def chrome_status_setter(status):
test = open(chrome_status, "w+")
if status:
process_name = "chrome.exe"
for proc in psutil.process_iter():
if proc.name() == process_name:
test.write(str(proc) + "\n")
test.close()
async def chrome_status_checker():
test = open(chrome_status, "r+")
status = test.read()
process_name = "chrome.exe"
true = []
false = []
for proc in psutil.process_iter():
if proc.name() == process_name:
if str(proc) in status:
true.append(str(proc))
else:
false.append(str(proc))
if len(true) > len(false):
return True
else:
return False
在Automate.py
class Automate(object):
def __init__(self):
self.options = webdriver.ChromeOptions()
loop_main = asyncio.new_event_loop()
loop_main.run_until_complete(singleton.initialize())
loop_main.close()
self.options.add_argument('--user-data-dir=F:\profile path')
self.options.add_experimental_option('debuggerAddress', 'localhost:9222')
self.driver = webdriver.Chrome(executable_path="F:\driver path", options = self.options)
这对我有用。如果有更好更简单的解决方案,请告诉我。
对我有用的更简单的解决方案(至少到目前为止)。 Python 允许您轻松导入 - 如果您正在下降 directories/packages。我正在使用包。我创建了一个简单的驱动程序文件,我在其中引用了驱动程序路径。然后我将该文件导入到位于 'one step up in directory.'
的包中在我想重新使用驱动程序的包中,我调用“from 'package.name' import 'driver'
然后我可以在我的脚本中使用驱动程序。