how to set page load timeout for new tab

Viewed 510
browser=webdriver.Firefox()
browser.set_page_load_timeout(4)
try:
    browser.get("https://9gag.com")
except:
    print("Page took too much time to load")

When I run the above code it will execute except: block always and prints "Page took too much time to load"

But when I run the code below in which I create a page where there is a button with id newPage and it opened the link(9gag) in new tab as soon as it is clicked. I don't get any exception.It take complete time to load and then print the url of the website

browser=webdriver.Firefox()
browser.get("file:///F:/documents/html_level_one/link_to_extreme_page_load.html")
browser.set_page_load_timeout(4)
browser.find_element_by_id("newPage").click()
browser.switch_to_window(browser.window_handles[-1])
print(browser.current_url)

I am using Firefox 28 and selenium 2.41

my very simple starting page

1 Answers

Page load timeouts don't apply to clicked elements because Selenium can't know if a new page is being loaded. That's why the page load timeout isn't applied. You could create a WebDriverWait and wait for an element on the page to be visible, etc. and set a timeout that way.

Related