0

So my question deals with iterating over multiple open windows at a time. Suppose I am opening a website (best example would be Naukri).

As soon as the page loads, two more pop up windows open up. My thought was to iterate on these open windows and get the title of each one of them.

Here is what I wrote :

 driver.get('http://www.naukri.com/')
 WebDriverWait(driver,15).until(lambda d: len(driver.window_handles)==3)
 print(driver.title)
 driver.switch_to.window(driver.window_handles[1])
 print(driver.title)
 driver.switch_to.window(driver.window_handles[2])
 print(driver.title)`

Now my question is that- this works, as far as we know that three windows are active. In this case, we don't know the exact number, how are we going to iterate through them? I thought of using this:

for handle in driver.window_handles:
 driver.switch_to.window(handle)

But since the window name changes every time the page loads, this becomes unusable.

I also tried this :

list1=[]
list1=driver.window_handles
for x in list1:
 print(driver.title)

but this prints the first window title three times instead. What should be my approach here?

gardo
3252 silver badges14 bronze badges
asked Jun 25, 2015 at 13:42

1 Answer 1

0

you need to switch to desired window before printing the title:

handles = driver.window_handles
for ii, hh in enumerate( handles ):
 driver.switch_to.window(hh)
 print 'window %s has title %s' % (ii, driver.title)
answered Jun 25, 2015 at 13:48
2
  • not sure if I understand the last i. I tried the list one with i and it says list can't have str indices. Commented Jun 25, 2015 at 15:16
  • variable i was loop counter. I wrote whole loop for you Commented Jun 25, 2015 at 15:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.