尝试使用Selenium,Python,Firefox和Xvfb运行测试。在运行测试几个小时后,我经常遇到浏览器无法创建和耗尽磁盘空间的问题。我读到的任何地方都说使用browser.quit()来释放tmp,但仍然遇到磁盘空间问题。然后,最重要的是,不得不将浏览器创建粘贴到while循环中,只是为了让它每次都创建。在测试访问的每个链接之后,我让它退出浏览器并打开一个新的。如果有人有任何想法我为什么遇到这些问题我会非常感激。
import time
import re
from selenium import webdriver
from pyvirtualdisplay import Display
from decimal import Decimal
display = Display(visible=0, size=(800, 600))
display.start()
links = tuple(open('links.txt'))
for link in links:
#Try to create browser
while True:
try:
browser = webdriver.Firefox()
except Exception:
time.sleep(1)
else:
break
#Navigate to link
try:
browser.get(link)
except Exception:
time.sleep(1)
else:
#Switch to first iframe
try:
ele = browser.find_element_by_xpath('/html/body/iframe')
browser.switch_to_frame(ele)
except Exception:
time.sleep(1)
else:
time.sleep(1)
#Switch to second iframe
try:
ele = browser.find_element_by_xpath('/html/body/iframe[1]')
browser.switch_to_frame(ele)
except Exception:
time.sleep(1)
else:
time.sleep(1)
#Wait for timer, if one is found
while True:
try:
wait = browser.find_element_by_id('one')
wait = wait.text
wait = re.sub("\D", "", wait)
wait = Decimal(timer)
except Exception:
break
else:
if wait == 0:
break
else:
time.sleep(1)
#Click second element
try:
ele = browser.find_element_by_id('two')
ele.click()
except Exception:
time.sleep(1)
else:
time.sleep(1)
#Quit browser if one exist
try:
browser.quit()
except Exception:
time.sleep(1)
else:
time.sleep(1)