我正在使用 Selenium 来模拟用户,问题是当使用 Selenium 启动 FireFox 实例时,它会创建临时配置文件。因此,为了保存 Cookie,浏览我的模拟用户,我必须用临时配置文件覆盖我的原始 Firefox 配置文件。所以我在关闭驱动程序时编写了该函数,应该复制配置文件:
def closeDriver(self):
time.sleep(2)
mozprofile = self.driver.capabilities["moz:profile"]
print(mozprofile)
try:
os.remove(mozprofile + "/parent.lock")
except:
pass
print('Admin?'+is_admin())
path = profilePath + self.profileName
if os.path.exists(mozprofile):
if os.path.exists(path):
shutil.rmtree(path)
time.sleep(0.5)
#shutil.copytree(mozprofile,path) #error 13
time.sleep(0.5)
shutil.copytree(mozprofile, path, ignore = shutil.ignore_patterns("parent.lock","lock", ".parentlock"))
time.sleep(3)
self.driver.quit()
logging.info("driver closed for profile:"+ self.profileName)
上面的代码单独调用时没有问题,但是在这段代码中使用时:
while running:
ranNrUser= random.randint(0,len(users)-1)
# user = users[ranNrUser]
user = user5
userSimulation =thirdPartySimulation.simulation(user.useragent,user.profilename)
ranNrTerms = random.randint(0,len(user.searchTerms)-1)
searchTerms = user.searchTerms[ranNrTerms]
searchTermsItems = user.searchTermsItems[ranNrTerms]
ranNrRepeats = random.randint(3,15)
count = 0
print(type(user))
while ranNrRepeats> count:
ranNrSearchTerm = random.randint(0,len(searchTerms)-1)
ranNrSearchTermItems= random.randint(0,len(searchTermsItems)-1)
choice = random.randint(0,4)
if choice == 0:
userSimulation.useInstagramSearch(searchTerms[ranNrSearchTerm])
if choice ==1:
userSimulation.useEbaySearch(searchTermsItems[ranNrSearchTermItems])
if choice == 2:
userSimulation.searchAmazon(searchTermsItems[ranNrSearchTermItems])
if choice ==3:
userSimulation.searchBing(searchTerms[ranNrSearchTerm])
userSimulation.searchEcosia(searchTerms[ranNrSearchTerm])
count=count+1
userSimulation.closeDriver()
出现以下回溯,所以在这个过程中甚至原始配置文件也被删除了,是不是因为while循环,我尝试了timee.sleep ffor the shutil,这是Windows问题吗?
Traceback (most recent call last): File "C:\Users\User\PycharmProjects\YouTubePersonalisierung\main.py", line 141, in performSession.performSession() File "C:\Users\User\PycharmProjects\YouTubePersonalisierung\performSession.py", line 46, in performSession userSimulation.closeDriver() File "C:\Users\User\PycharmProjects\YouTubePersonalisierung\thirdPartySimulation.py", line 85, in closeDriver shutil.copytree(mozprofile, path, ignore = shutil.ignore_patterns("parent.lock","lock", ".parentlock")) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 554, in copytree return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks, File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 510, in _copytree raise Error(errors) shutil.Error: [('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\cookies.sqlite-shm', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\cookies.sqlite-shm', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\cookies.sqlite-shm'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\cookies.sqlite-wal', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\cookies.sqlite-wal', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\cookies.sqlite-wal'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\favicons.sqlite-shm', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\favicons.sqlite-shm', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\favicons.sqlite-shm'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\favicons.sqlite-wal', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\favicons.sqlite-wal', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\favicons.sqlite-wal'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\places.sqlite-shm', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\places.sqlite-shm', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\places.sqlite-shm'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\places.sqlite-wal', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\places.sqlite-wal', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\places.sqlite-wal'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\webappsstore.sqlite-shm', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\webappsstore.sqlite-shm', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\webappsstore.sqlite-shm'"), ('C:\\Users\\User\\AppData\\Local\\Temp\\rust_mozprofileBnVE2W\\webappsstore.sqlite-wal', 'C:/Users/User/AppData/Roaming/Mozilla/Firefox/Profiles/\\b81eyp7s.ThirdPartyUser5\\webappsstore.sqlite-wal', "[Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofileBnVE2W\\\\webappsstore.sqlite-wal'")