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")
Sign up to request clarification or add additional context in comments.

2 Comments

My browser opens and says page not found on the server. It doesn't open google.com
@JoeMcKinzie okay, updated the answer, please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.