Add ugly timeout hack to get_foreground to deal with deeply nested elements.