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?

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)
  • not sure if I understand the last i. I tried the list one with i and it says list can't have str indices. – demouser123 Jun 25 '15 at 15:16
  • variable i was loop counter. I wrote whole loop for you – Peter M. Jun 25 '15 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.