English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When the company performs codereview, it limits the time to view the code, in fact, many codes are automatically generated by the framework and do not require much time to view. In order to meet the requirements, you need to spend some time (clicking on the fixed area of the web page). I thought I could use the means of automated testing to complete this kind of ineffective physical labor.
First, clarify the requirements:
I thought of two solutions, sikuli or python+selenium. The advantage of sikuli is simple and direct logical operations, using images as indicators, the disadvantage is that the window needs to be fixed and cannot run in the background. Selenium is a bit more complex, but runs fast, and the window can be obscured.
Here is a simple record of using Python+A small example of selenium.
The version of Python used is3.3and selenium2,Windows environment (now supported3.0 and above, many forums and blogs have not been updated).
Firstly, the installation of the software, Python is not elaborated on, remember to set the environment variables.
Next, install selenium, if pip has been installed. Just run the following command.
pip install -U selenium
Another way is,https://pypi.python.org/packages/source/s/selenium/selenium-2.52.0.tar.gzDownload and extract. Here is a brief description of the Windows version, actually it is more or less the same under Unix, use Wget to download and install.
使用命令(setup一般用于第三方模块的安装):
cd c:\Python3\xxxx python setup.py install
安装过程中可能会出现ImportError: No module named setuptools,这是因为缺少setuptools模块,Python默认不安装。
在http://pypi.python.org/pypi/setuptools上面提供了各系统的安装包和安装指南,对于Windows系统,下载https://bootstrap.pypa.io/ez_setup.py自动化安装脚本。
运行:
python ez_setup.py
完成后再安装selenium即可。
这里用我自己的实例简单的讲解一下流程。
第一步先完成打开浏览器。
selenium2结合了selenium和webdriver,直接引入各个浏览器相应的驱动,打开即可,注意chrome驱动可能需要另行安装。
from selenium import webdriver browser = webdriver.Firefox() browser.get('https://www.xxx.com')
打开网页后需要登录,F12打开浏览器调试器,小箭头选取元素,查看登录框账号和密码的属性,一般都有ID。selenium可以通过以下各个方法获取元素并进行各种操作,具体解释请看上方链接文档:
其中id最为有效方便,优先考虑。选取完元素之后可以通过WebDriver API调用模拟键盘的输入和鼠标的点击操作。代码如下:
username="qun" passwd="passwd" browser = webdriver.Firefox() browser.get('https://www.xxx.com') browser.implicitly_wait(10) elem=browser.find_element_by_id("loginFormUserName") elem.send_keys(username) elem=browser.find_element_by_id("loginFormPassword") elem.send_keys(passwd) elem=browser.find_element_by_id("loginFormSubmit") elem.click()
一般登录之后页面都会跳转到新的网页上,如何获取新的网页呢?这里有个窗口句柄的概念,通过切换窗口句柄来完成。注意!有时候元素在一个frame里面的时候,也需要通过swtich切换。这里出现一个wait函数(上面也有),是因为页面加载需要时间,很可能在点击之后元素才加载出来,下一节详细解释一下wait。
browser.implicitly_wait(10) browser.switch_to_window(browser.window_handles[-1")
之后选择需要点击的区域,这里使用 xpath定位,因为在自动化测试的过程中,很可能元素无法通过id,name等方法定位(不少人就是不写,爱table套table,我也么办法),xpath就有了用武之地。常用的偷懒方法是Firefox安装xpath插件,右键直接获取。这里不介绍,因为不提倡,使用插件会造成代码里充斥这样的东西:
XPath(/html/body/div/div[3]/div[2]/div[4]/p[2")
不到万不得已,尽可能使用元素的特征来定位,比如按钮的name。
或者通过父元素定位子元素。
username =browser.find_element_by_xpath("//input[@name='username']") clear_button = browser.find_element_by_xpath("//form[@id='loginForm']/input[4")
代码如下,网上经常出现的By的用法需要引入包。
from selenium.webdriver.common.by import By
这里使用另外一个函数,我不知道有什么区别- -。
for i in range(100): elem=WebDriverWait(browser, 30).until( lambda x:x.find_element_by_xpath("//table[@class='aaa']"/td[1)) elem.click() time.sleep(20) print ("click",i)
然后这里还要提一下wait函数,在selenium2中延迟等待分为两种,显示等待和隐式等待。
显示等待
显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception。
element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "myDynamicElement")) )
隐式等待
注意,隐式等待是告诉设置所有dom元素在寻找某个元素的时候,如果没立即找到,再尝试这么长时间。
browser.implicitly_wait(10) # seconds
两者的区别在于一个是自己直接管理超时对象,一个是交给webdriver去做。
当然也可以用等待的方法干等。记得引入time包。
time.sleep(20)
这里只是简单演示一下用法,有许多可以改进的地方。没有封装函数,没有使用多线程并发执行多个例程。以后有需求再改进。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来自网络,版权归原作者所有。内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#oldtoolbag.com(在发送邮件时,请将#替换为@进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权内容。)