标签:UI put cut _for ant class 如何 一个 color
WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法。
定位单个页面元素(返回单个元素对象)
需要注意的是,上面的方法当匹配到多个对象时,只能返回定位到的第一个元素对象,当没有匹配到任何元素对象,则会抛出异常NoSuchElementException
定位页面元素组(返回元素对象列表)
HTML模板Example1
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> <html>
login_form = driver.find_element_by_id(‘loginForm‘)
username = driver.find_element_by_name(‘username‘) password = driver.find_element_by_name(‘password‘)
login_form = driver.find_element_by_xpath("/html/body/form[1]") login_form = driver.find_element_by_xpath("//form[1]") login_form = driver.find_element_by_xpath("//form[@id=‘loginForm‘]")
username = driver.find_element_by_xpath("//form[input/@name=‘username‘]") username = driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[1]") username = driver.find_element_by_xpath("//input[@name=‘username‘]")
clear_button = driver.find_element_by_xpath("//input[@name=‘continue‘][@type=‘button‘]") clear_button = driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[4]")
HTML模板Example2
<html> <body> <h1>Welcome</h1> <p class="content">Are you sure you want to do this?</p> <a href="continue.html" name="tj_continue" title="web" class="RecycleBin xz">Continue</a> <a href="cancel.html">Cancel</a> </body> <html>
continue_link = driver.find_element_by_link_text(‘Continue‘)
continue_link = driver.find_element_by_partial_link_text(‘Conti‘)
heading1 = driver.find_element_by_tag_name(‘h1‘)
content = driver.find_element_by_class_name(‘content‘)
content = driver.find_element_by_css_selector(‘p.content‘)
continue_link = driver.find_element_by_css_selector("a[name=\"tj_continue\"]")
continue_link = driver.find_element_by_css_selector(‘a[title="web"]‘)
continue_link = driver.find_element_by_css_selector(‘a.RecycleBin‘)
Selenium with Python 003 - 页面元素定位
标签:UI put cut _for ant class 如何 一个 color
原文地址:http://www.cnblogs.com/guanfuchang/p/7405677.html