Fenixs
Fenixs

Reputation: 353

switching between windows when target name is different for different links

I have a code where I need to click on number links, proceed to the new window opened, assert something and go back to the main window and click on next link. Not all the links have same 'target' with which I am identifying the new window.
As of now i have 3 possible targets for all the links available.

Is there any easy way to implement this need other than the following:

   try:
     driver.switch_to_window("windowName1")
   except:
     pass

   try:
     driver.switch_to_window("windowName2")
   except:
     pass  

   try:
     driver.switch_to_window("windowName3")
   except:
     pass  

   self.assertIn('info', self.driver.title)
   #go back to the main window

Upvotes: 1

Views: 57

Answers (1)

falsetru
falsetru

Reputation: 369324

Iterate a list of (windows, targets):

targets = [
    ['windowName1', 'target1'],
    ['windowName2', 'target2'],
    ['windowName3', 'target3'],
]

for window_name, target_name in targets:
    try:
        driver.switch_to_window(window_name)
    except: # OR except InvalidSwitchToTargetException:
        continue
    # Do something with window_name, target_name ...

Upvotes: 1

Related Questions