标签:内容 password splay sub 列表 word NPU his xpath
3,定位元素
测试对象的定位和操作是webdriver的核心内容
定位对象的目的一般有下面几种
操作对象
获得对象的属性,如获得测试对象的class属性,name属性等等
获得对象的text
获得对象的数量
webdriver提供了一系列的对象定位方法,常用的有以下几种
id
name
class name
link text
partial link text
tag name
xpath
css selector
以下方法返回单个元素
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
返回元素列表:
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
两个私有方法:
find_element and find_elements
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
By class可以使用的属性
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
示例
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
<html>
获取form元素使用方法:
login_form = driver.find_element_by_id('loginForm')
示例
<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>
获取username 和password 元素使用方法:
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
获取到login按钮,因为他在clear按钮之前
continue = driver.find_element_by_name('continue')
XPath is the language used for locating nodes in an XML document
示例html
<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>
定位form 元素:
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 元素
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按钮:
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>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
continue.html link can be located like this
continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
<html>
<body>
<h1>Welcome</h1>
<p>Site content goes here.</p>
</body>
<html>
#定位:
heading1 = driver.find_element_by_tag_name('h1')
<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html>
#定位:
content = driver.find_element_by_class_name('content')
<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html>
#定位:
content = driver.find_element_by_css_selector('p.content')
>>> text = driver.find_element_by_class_name("tlid-translation")
>>> text.
text.clear(
text.find_element_by_partial_link_text(
text.find_elements_by_name(
text.is_enabled(
text.screenshot_as_png
text.click(
text.find_element_by_tag_name(
text.find_elements_by_partial_link_text(
text.is_selected(
text.send_keys(
text.find_element(
text.find_element_by_xpath(
text.find_elements_by_tag_name(
text.location
text.size
text.find_element_by_class_name(
text.find_elements(
text.find_elements_by_xpath(
text.location_once_scrolled_into_view
text.submit(
text.find_element_by_css_selector(
text.find_elements_by_class_name(
text.get_attribute(
text.parent
text.tag_name
text.find_element_by_id(
text.find_elements_by_css_selector(
text.get_property(
text.rect
text.text
text.find_element_by_link_text(
text.find_elements_by_id(
text.id
text.screenshot(
text.value_of_css_property(
text.find_element_by_name(
text.find_elements_by_link_text(
text.is_displayed(
text.screenshot_as_base64
页面上有很多个属性基本相同的元素,现在需要具体定位到其中的一个。由于属性基本相当,所以在定位的时候会有些麻烦,这时候就需要用到层级定位。先定位父元素,然后再通过父元素定位子孙元素
如何通过层级定位来定位下拉菜单中的某一项。由于两个下拉菜单中每个选项的link text都相同,href也一样,所以在这里就需要使用层级定位了。
具体思路是:先点击显示出1个下拉菜单,然后再定位到该下拉菜单所在的ul,再定位这个ul下的某个具体的link。在这里,我们定位第1个下拉菜单中的Another action这个选项
标签:内容 password splay sub 列表 word NPU his xpath
原文地址:https://www.cnblogs.com/g2thend/p/12493555.html