Selenium Webdriver with Python regularly fails to launch the Firefox browser, and a retry is often needed. Usually a single or second retry is sufficient, so I do the following:
try:
self.driver = webdriver.Firefox()
except WebDriverException, e:
print "Unable to load profile, retrying"
try:
self.driver = webdriver.Firefox()
except WebDriverException, e:
print "Unable to load profile, retrying"
self.driver = webdriver.Firefox()
This is no longer serving me, as it is now regularly taking 3 or more retries before the browser launches. What is the neatest way to incorporate a while
loop such that it keeps retrying until the browser successfully loads (at which point self.driver
will exist as an object)?
Note: I know that there is a slight chance that an infinite loop may be encountered, but for the purposes of supplying suggestions, you may disregard this fact.
How about defining self.driver
as None
before the while
loop and then looping till its not None
? Example -
self.driver = None
while not self.driver:
try:
self.driver = webdriver.Firefox()
except WebDriverException, e:
print "Unable to load profile, retrying"
You can use python's else
clause. Check python's exception handling
while True:
try:
self.driver = webdriver.Firefox()
except WebDriverException, e:
print "Unable to load profile, retrying"
else:
break
It is very interesting for you to say loading Firefox will often fail, it has never happened to me before. I have been running Selenium tests for a while now. You may want to investigate why your Firefox driver keeps failing, does it need an update?
I suggest you do implement a limit on how many time it will retry.
numberOfRetry = 5
while (driver==None):
try:
driver = webdriver.Firefox()
except:
numberOfRetry-=1
if numberOfRetry <= 0:
logging.critical("Maximum number of retry reached")
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.