Selenium + Python multi-browser test
Support Library Packages
Before learning Python + Selenium, let's look at support for multi-browser simulations. Webdriver,help (Webdriver) is currently included in the selenium package to view the packages below, or you can view the source files
Start Firefox
Firefox is a more mature browser selenium, many of the new features will be reflected in Firefox. But do the page test, startup speed is relatively slow, start after the speed is acceptable. To start the Firefox browser directly, see the following code:
# !/usr/bin/env python # -*-coding:utf-8-*- from Import = webdriver. Firefox () browser.get ('http://www.baidu.com')
Note: Your computer needs to be installed Firefox, after execution will open a new Firefox window, and do the operation of opening Baidu homepage.
Start Chrome
You need to download chromedriver,google:http://chromedriver.storage.googleapis.com/index.html to start chrome; pan.baidu.com/s/1dd8mn1f
Put the EXE in a chrome package, as
Add the address to the PATH environment variable, such as: D:\chrome (X64) -38.0.2125.101\chrome-bin
Similarly, start Chrome to open the Baidu home page, see the following code:
# !/usr/bin/env python # -*-coding:utf-8-*- from Import = webdriver. Chrome () browser.get ('http://www.baidu.com')
If you're still doing something wrong, it's recommended that you review the chrome version, which may be a problem with chrome itself, or see how to use OS variables below.
Start IE
Start IE similar to chrome, need to download iedriver,google:https://code.google.com/p/selenium/downloads/list; it's forbidden:
Similar operation will put this exe into IE package and add path, the code with chrome, the difference is only browser = Webdriver. Ie () ;
If the environment variable starts the Webdriver way has the problem, the code executes the error, may use the OS variable way, the reference following code (chrome also may use similar code):
#!/usr/bin/env python#-*-coding:utf-8-*- fromOsImportenviron fromSeleniumImportWebdriveriedriver="C:\Program files\internet Explorer\iedriverserver.exe"environ["Webdriver.ie.driver"] =Iedriverdriver=Webdriver. Ie (Iedriver) driver.get ("http://www.baidu.com")
Multi-browser code optimization
In addition to Firefox, Chrome, IE, can see Opera, Safari and so can also support, this is not introduced here, if you want to know, can Google
You can write a. py to launch the browser, the test code only needs to import, see the following test framework related chapters.
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 fromOsImportenviron5 fromSeleniumImportWebdriver6 7FF = Webdriver. Firefox ()#Firefox8Ff.maximize_window ()#Full-window9 TenChromedriver ="C:\Users\sisi\AppData\Local\Google\Chrome\Application\chromedriver.exe" Oneenviron["Webdriver.chrome.driver"] =Chromedriver AChrome = Webdriver. Chrome (Chromedriver)#Chrome -Chrome.maximize_window ()#Full-window - theIedriver ="C:\Program files\internet Explorer\iedriverserver.exe" -environ["Webdriver.ie.driver"] =Iedriver -ie = Webdriver. Ie (Iedriver)#IE -Ie.maximize_window ()#Full-window + - + " "Your Testing code should is here" " A atTest_url ="http://www.baidu.com" - - ff.get (Test_url) - chrome.get (Test_url) -Ie.get (Test_url)
Selenium + Python multi-browser test