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?
1 Answer 1
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– demouser1232015年06月25日 15:16:17 +00:00Commented Jun 25, 2015 at 15:16
-
variable i was loop counter. I wrote whole loop for youPeter M. - stands for Monica– Peter M. - stands for Monica2015年06月25日 15:38:47 +00:00Commented Jun 25, 2015 at 15:38