I have the following code for when I need to click on an item, which opens in a new tab,and I need to switch the browser to that tab.
def ThingsinTab(browser):
clickbutton=Somefunction #Find where to click
clickbutton.click()
try:
WebDriverWait(browser,10).until(
EC.number_of_windows_to_be(len(old_tabs)+1))
except: return browser
THIS POINT!!
new_window=(set(browser.window_handles)-old_tabs).pop()
browser.switch_to.window(new_window)
#Do some stuff with this tab
browser.close()
browser.switch_to_window(main_tab)
return browser
Sometimes the tab opens, but it remains with a grey background, not fully loaded for a long while. I have narrowed where the code is stuck in this point and I signaled it in the code above. What I want is a way to time the new variable new_window
and the switch, so that if let's say, after 10 seconds it hasn't move on in the code, it stops, closes the new tab and return the browser.
Provided this function is a part of a bigger code, I would like to know as well how to time in the bigger script this function, so if it takes more than a number of seconds, it interrupts the function from running and resume the rest of the code.
Big script
some things
ThingsinTab() #I want to set a maximum time for this function to complete its code,or else stop it and move on.
More some things
You could spin off a thread that opens the window, then wait for a maximum of 10 seconds. If it hasn't finished by then, kill the thread and perform your alternative strategy.
You might find the threading
module useful. thread.join(10)
can do the "wait for a maximum of 10 seconds" part; it shouldn't be too hard to put this together, aside from the usual difficulty of getting threads to work.
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.