Selenium use and principle

1. Introduction to Selenium

Selenium is a web testing tool that implements web testing by directly controlling the browser, exactly the same as real user operations. Selenium currently supports browsers such as IE, Firefox, Chrome, Safari, Opera, etc. Selenium supports mainstream operating system platforms - Windows, Linux, Mac, etc., Selenium supports Java, Ruby, Python, Perl, PHP, C# and other programming languages ​​for testing . Selenium IDE can also record the user's basic operations, which is used to facilitate production test cases, which can be played back during subsequent tests.

2. Simple use based on Python

Below is a simple example to open Baidu and search

from selenium import Webdriver
from selenium.webdriver.common.keys import Keys

#启动web driver server,打开IE浏览器
ie = Webdriver.Ie()
#打开百度首页
ie.get('http://www.baidu.com')
#获取搜索输入框
kw = ie.get_element_by_id('kw')
#在输入框输入selenium,并回车
kw.send_keys('selenium'+Keys.RETURN)
#ie.close()

3. Remotely call the browser

Selenium is actually composed of a server and a client. The server must be on the same host as the called browser. The server controls the browser's behavior through webdriver. The client can be located on a different host from the server, as long as the network can You can access it. The client communicates with the server through the http protocol (restful) and sends control commands to the server. Because the client interacts with the server through the http protocol, the client can be implemented in any programming language that supports tcp/ip.

from selenium.webdriver.remote.webdriver import Webdriver as RemoteWebDriver
from selenium.webdriver.common.keys import Keys

#启动web driver server,打开IE浏览器
browser = RemoteWebdriver('http://remoteHost:remotePort')
#打开百度首页
browser .get('http://www.baidu.com')
#获取搜索输入框
kw = browser .get_element_by_id('kw')
#在输入框输入selenium,并回车
kw.send_keys('selenium'+Keys.RETURN)
#browser.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324412103&siteId=291194637