1
from selenium import webdriver
w1=webdriver.Firefox()
def f1():
    w1.get("dagsb.com")
def f2():
    w1.get("google.com")

I have the above code snippet. I want to try to call f1() and if it throws an error (which it does as dagsb.com doesnt exist) I want to call f2()

How can I do so?

1 Answer 1

1

Use try and except to handle webdriver-specific errors:

from selenium.common.exceptions import WebDriverException

try:
    w1.get("http://dagsb.com")
except WebDriverException as e:
    # TODO: log exception
    w1.get("https://google.com")

Note that this is not going to handle Page not found 404 - since in that case, there would not be an exception thrown. The idea would be to check the page title not to equal "Page not found":

is_found = w1.title != page_not_found_message
if not is_found:
    w1.get("https://google.com")
2
  • My browser opens and says page not found on the server. It doesn't open google.com
    – user5852923
    Commented Jan 28, 2016 at 18:00
  • @JoeMcKinzie okay, updated the answer, please check.
    – alecxe
    Commented Jan 28, 2016 at 18:09