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?
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")
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.