selenium python browser multi window processing code example

  • 2020-06-23 01:08:46
  • OfStack

This paper mainly studies the multi-window processing of selenium python browser, and shares the operation example code, as follows:


#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'zuoanvip'
# In the testing process, we sometimes encounter multiple browser Windows, at which time we can manipulate the elements of different Windows through the handle of the window 
from selenium import webdriver
import os
import time

driver =webdriver.Firefox()
driver.get('http://www.baidu.com')


# Gets a handle to the current window 
nowwhandle = driver.current_window_handle

# Open a new registration window 
driver.find_element_by_name('tg_reg').click()

# Gets a handle to all Windows 
allhandles = driver.window_handles

# Loop to determine whether the window is the current window 

for handle in allhandles:
  if handle != nowwhandle:
    driver.switch_to_window(handle)
    print 'Now register window!'
    # Switch to the mailbox registration TAB 
    driver.find_element_by_id('mailRegTab').click()
    time.sleep(5)
    driver.close()

# Returns the window to which you came 
driver.switch_to_window(nowwhandle)

driver.find_element_by_id('kw').send_keys(u' Registered successfully ')
driver.quit()

The execution process is as follows: first get the handle of the current window through nowhandle, then open the registration window; Get all window handles via allhandles; Loop through the handle; Determine if the window is nowhandle, if not, get the handle to the current window, manipulate the elements of the current page, and finally return nowhandle

conclusion

That's it for the multi-window processing code example for the selenium python browser. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: