Selenium2+python Automation 71-switching between multiple browsers "reprint"

Source: Internet
Author: User

Objective

Sometimes the function of some business involves multiple systems, the need to create some data in the Web System 1, and then use some parameters are generated dynamically, you need to call the parameters inside the Web System 2.

To give a simple example: In doing certain business, the need for SMS verification code, I can not get a mobile phone attached to the computer bar, so silly, our goal is to obtain SMS verification code, SMS Verification code has a text message platform to query.

Of course, you can directly manipulate the database is the simplest, with Python database directly through SQL to check the line.

First, start two driver

1. If I want to start 2 Firefox, a Firefox open Baidu, another Firefox open blog Park, we only need to use 2 instances of driver to control the line

(Note: Do not two are called driver, otherwise the back will overwrite the front, resulting in the previous browser window can not continue to operate)

2. After running the result, the desktop launches 2 windows, one opens the Baidu, one opens the Shanghai-leisurely blog Garden

Second, the window is shut down

1.driver1 is the instance parameter that controls the first browser window, Driver2 is the instance parameter that controls the second window, and if you want to turn off the first one, Driver1.quit () is OK.

2.quit out of the first browser window, the front of the browser window will not be able to operate, here can proceed to the second browser window

# Coding:utf-8
From selenium import Webdriver
Import time

# Launch the first browser
Driver1 = Webdriver. Firefox ()
Driver1.get ("https://www.baidu.com")
Print (Driver1.title)

# Launch a second browser
Driver2 = Webdriver. Firefox ()
Driver2.get ("http://www.cnblogs.com/yoyoketang/")
Print (Driver2.title)

# Turn off the first browser window
Driver1.quit ()

# Point Home "blog Park" button
driver2.find_element_by_id ("Blog_nav_sitehome"). Click ()
Time.sleep (2)
Print (Driver2.title)

Third, package Start browser method

1. If you are involved in a different browser (such as Firefox, Chrome) between the switch, we can specifically write a function to launch a different browser

Iv. Reference Code

# Coding:utf-8
From selenium import Webdriver

def browser (browser= ' Firefox '):
‘‘‘
Open Browser "Firefox", "Chrome", "ie", "PHANTOMJS"
Usage
Driver = Broswer ("Chrome")
‘‘‘
Try
if browser = = "Firefox":
Driver = Webdriver. Firefox ()
Return driver
elif Browser = = "Chrome":
Driver = Webdriver. Chrome ()
Return driver
elif Browser = = "ie":
Driver = Webdriver. Ie ()
Return driver
elif Browser = = "Phantomjs":
Driver = Webdriver. PHANTOMJS ()
Return driver
Else
Print ("Not found browser! You can enter ' Firefox ', ' Chrome ', ' ie ' or ' phantomjs ')
Except Exception as msg:
Print "Open browser error:%s"% msg

if __name__ = = "__main__":
# default Startup Firefox
Driver_firefox = browser ()
Driver_firefox.get ("https://www.baidu.com")
Print ("Open browser:%s"% driver_firefox.name)
Print (Driver_firefox.title)

# Start Section Phantomjs
DRIVER_PJ = Browser ("Phantomjs")
Driver_pj.get ("http://www.cnblogs.com/yoyoketang/")
Print ("Open browser:%s"% driver_pj.name)
Print (Driver_pj.title)

Selenium2+python Automation 71-switching between multiple browsers "reprint"

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.