Sample Code for multi-window processing in selenium python browser, seleniumpython

Source: Internet
Author: User
Tags selenium python

Sample Code for multi-window processing in selenium python browser, seleniumpython

This article focuses on the multi-window processing of selenium python browsers and shares the operation instance Code as follows:

#! /Usr/bin/python #-*-coding: UTF-8-*-_ author _ = 'zuoanvip '# during the test, multiple browser windows may occur, at this time, we can use the window handle to operate the elements of different windows from selenium import webdriverimport osimport timedriver = webdriver. firefox () driver. get ('HTTP: // www.baidu.com ') # obtain the handle of the current window nowwhandle = driver. current_window_handle # Open the new registration window driver. find_element_by_name ('tg _ reg '). click () # obtain the handle allhandles = driver for all windows. window_handles # cyclically determine whether the window is the current window for handle in all Handles: if handle! = Nowwhandle: driver. switch_to_window (handle) print 'now register window! '# Switch to the mailbox registration tag driver. find_element_by_id ('mailregtab '). click () time. sleep (5) driver. close () # return the original window 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 obtain the handle of the current window through nowhandle, then open the registration window, get the handle of all windows through allhandles, traverse the handle cyclically, and determine whether the window is nowhandle, if not, obtain the handle of the current window, operate on the elements of the current page, and return nowhandle.

Summary

The above is all the content about the example code for multi-window processing in selenium python browser. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.