Selenium with Python 003 - 页面元素定位

WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法。

定位单个页面元素(返回单个元素对象)

  • 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

需要注意的是,上面的方法当匹配到多个对象时,只能返回定位到的第一个元素对象,当没有匹配到任何元素对象,则会抛出异常NoSuchElementException

定位页面元素组(返回元素对象列表)

  • 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

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>

通过id定位

login_form = driver.find_element_by_id(‘loginForm‘)

通过name定位

username = driver.find_element_by_name(‘username‘)
password = driver.find_element_by_name(‘password‘)

通过xpath定位

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‘)

通过class定位

content = driver.find_element_by_class_name(‘content‘)

通过css 选择器定位

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‘)
时间: 2024-08-21 13:48:25

Selenium with Python 003 - 页面元素定位的相关文章

【selenium学习笔记】webdriver进行页面元素定位

[selenium学习笔记]webdriver进行页面元素定位 进行Web页面自动化测试,对页面上的元素进行定位和操作是核心.而操作又是以定位为前提的,因此,对页面元素的定位是进行自动化测试的基础. 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素属性(class属性,name属性)等等.webdriver就是利用元素的这些属性来进行定位的. 可以用于定位的常用的元素属性: id name class name tag name link text partial link te

Python3.x:Selenium中的webdriver进行页面元素定位

Python3.x:Selenium中的webdriver进行页面元素定位 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素属性(class属性,name属性)等等.webdriver就是利用元素的这些属性来进行定位的. 可以用于定位的常用的元素属性: id name class name tag name link text partial link text xpath css selector 对应于webdriver中的定位一个元素方法分别是: driver.find_e

java selenium webdriver实战 页面元素定位

自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选择下拉列表中的那个下拉列表或者输入框中输入什么值 其中定位页面元素是三步骤的第一步,本篇介绍常用的定位方法 webDriver对象的findElement函数用于定位一个页面元素,findElements函数用户定位多个页面元素,定位的页面元素使用webElement对象进行存储 常用的方法有: 1

【Selenium专题】高亮显示页面元素

高亮显示页面元素主要用到Selenium中使用js的知识点,最常用的是检查元素定位是否正确.此外,实现js的调用大大增强了Selenium的功能.以下是调试通过的案例: import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class js { public static

【前端经纬】将页面元素定位

最近在做公司的项目的手,遇到了要是 html页面元素定位的问题. 查阅了相关资料,这里 Mark.  1)可以使用 css的position属性实现该功能. 2)代码实现如下(可以运行 http://www.w3school.com.cn/tiy/t.asp?f=csse_position_absolute): <html> <head> <style type="text/css"> h2.pos_abs { position:absolute;

Robot Framework 教程 (2) - 页面元素定位

上一篇文章中,简单模拟了一个baidu搜索并截图的过程,在搜索过程中,我们需要对搜索框.搜索按钮进行定位.本篇文章主要介绍一下具体的定位方法. 我们的脚本如下: *** Settings *** Library Selenium2Library *** Test Cases *** firefox兼容性 Open Browser https://www.baidu.com/ ff Input Text id=kw LeetTest Click button id=su Sleep 2 Captu

UI自动化页面元素定位

做ui自动化的最基础的就是页面元素定位了,如果连页面元素都定位不到,自动化从何谈起呢?接下来我们就看看页面元素定位的方法吧!(这里就用百度页面来进行演示) 一.最通用的几种定位方式: 1.通过id定位 1 driver.find_element_by_id('su') 2.通过name定位 driver.find_element_by_name('mp') 3.通过class属性 driver.find_element_by_class_name('s_ipt') 4.通过文本链接 driver

python selenium系列(二)元素定位方式

一 前言 元素定位,是操作元素的第一步,也是WebUI自动化的难点和核心. 二 元素定位方法 selenium提供了内置的方法完成对待操作元素的定位,主要分为8类,其中,每类又可细分为定位单个元素和定位多个元素,另外还提供了2个私有方法.详细如下: 定位单个元素 ·         find_element_by_id ·         find_element_by_name ·         find_element_by_xpath ·         find_element_by_

Python + Selenium(三)网页元素定位(二)id 定位

了解了开发者工具和 HTML 结构 后,接下来,就来看看在 Python + Selenium 中如何查找元素了. 查找元素,是 WebDriver 中的核心部分之一.WebDriver提供了八种定位方法,方法如下: 定位方式 方法 说明 id find_element_by_id() 使用 id 属性定位 name find_element_by_name() 使用 name 属性定位 class_name find_element_by_class_name() 使用 class 属性定位