Reputation: 22043
I am working on selenium to retrieve learning stuff:
During the executing of my code, the target website will defensively open a new tab to stop my process.
I tried to check the current_url
, but it does not work as expected
if browser.current_url != start_url:
browser.switch_to.window(browser.window_handles[0])
How could I check like this:
if browser.current_tab != tab[0]:
browser.switch_to.window(browser.window_handles[0])
Upvotes: 0
Views: 783
Reputation: 1439
driver.current_window_handle
returns a unique string identifying to your currently opened tab. So you can first store the desired tab somewhere and then compare it to the current tab whenever you want:
desired_tab = driver.current_window_handle #storing the handle in a variable
if driver.current_window_handle != desired_tab:
driver.switch_to_window(desired_tab) #switching to the tab in case it's not
Upvotes: 1