3. 定位单个对象

以下方法都可以用来定位某个对象,优先选择id,name.

  • 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

  1. Location by ID

    login_form = driver.find_element_by_id(‘loginForm‘)
  2. Location by Name
    username = driver.find_element_by_name(‘username‘)
    password = driver.find_element_by_name(‘password‘)

  3. Location by XPATH
      绝对路径(如何html有轻微的调整就会失效)
    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‘]")
    
    相对路径子元素的name属性username = driver.find_element_by_xpath("//form[input/@name=‘username‘]")相对路径id熟悉后index
    username = driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[1]")相对路径name属性
    username = driver.find_element_by_xpath("//input[@name=‘username‘]")
    
    相对路径中的双属性name和typeclear_button = driver.find_element_by_xpath("//input[@name=‘continue‘][@type=‘button‘]")相对路径中前id属性后index
    clear_button = driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[4]")
     

    There are also a couple of very useful Add-ons that can assist in discovering the XPath of an element:

    • XPath Checker - suggests XPath and can be used to test XPath results.
    • Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.
    • XPath Helper - for Google Chrome
  4. Location Hyperlink by Link Text
    continue_link = driver.find_element_by_link_text(‘Continue‘)
    continue_link = driver.find_element_by_partial_link_text(‘Conti‘)
  5. Location by Tag Name
    heading1 = driver.find_element_by_tag_name(‘h1‘)
  6. Location by Class Name
    content = driver.find_element_by_class_name(‘content‘)

    
    
  7. Location by CSS Selector
    content = driver.find_element_by_css_selector(‘p.content‘)


    Sauce Labs has good documentation on CSS selectors.

3. 定位单个对象

时间: 2024-08-26 08:49:16

3. 定位单个对象的相关文章

selenium python (六)定位一组对象

checkbox源码: <html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>Checkbox</title><script type="text/javascript" async="" src="https://ajax.goog

selenium python 定位一组对象

为什么定位一组对象? 定位一组对象的思想 在定位一组对象的过程中我们如何实现?以前的都是通过具体的对象定位,那么定位一组我们就需要通过css来定位   在单个定位对象中使用的是find_element_by_id()  但是定位一组对象需要使用find_elements_by_css_selector eg: #定位所有的checkbox对象 checkboxs = dr.find_elements_by_css_selector('input[type=checkbox]') for chec

selenium(python)下定位单个元素的常用方法

定位单个元素的常用方法 以百度搜索框额面为例,蓝色位置为定位到输入框的属性: <input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"> 1.       用id定位元素:find_element_by_id() 从定位到的属性可以看到一个id属性:id="k

Objective-C 【单个对象内存管理】

------------------------------------------- 单个对象内存管理 (1)野指针 ①定义了一个指针变量,但是并没有赋初值,它随机指向一个东西 ②某指针变量指向的内存空间被释放掉了(指向僵尸对象的指针) (2)僵尸对象 已经被销毁的对象(无法被使用的对象) (3)空指针 没有指向存储空间的指针(里面存的是nil,也就是0) 给空指针发消息是没有任何反应的,不会提示出错 代码: #import <Foundation/Foundation.h> @interf

小白学开发(iOS)OC_ 单个对象的内存管理(2015-08-02)

// //  main.m //  单个对象的内存管理 // //  Created by admin on 15/8/3. //  Copyright (c) 2015年 admin. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" void test(); void test5(Person * p); int main(int argc, const char *

单个对象内存管理

前文我们已经说了内存管理的原则:只要出现了new alloc retain ,就一定会配对出现一个release,autorelease 那么当我一个对象被释放之后,如果我们再次调用这个对象的方法(包括这个对象的retain release方法),将会出现 野指针 错误,在xcode中具体表现为 EXC_BAD_ACCESS:访问了不可访问的内存空间,就会导致这个错误. 下面这个例子#import <Foundation/Foundation.h> int main (int argc, co

10,组合模式(Composite Pattern)是将对象组合成树形结构以表示“部分--整体”的层次结构。使得用户对单个对象和组合对象的使用具有一致性。

Composite模式也叫组合模式,是构造型的设计模式之一.通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树. Component (树形结构的节点抽象) - 为所有的对象定义统一的接口(公共属性,行为等的定义) - 提供管理子节点对象的接口方法 - [可选]提供管理父节点对象的接口方法  Leaf (树形结构的叶节点) Component的实现子类  Composite(树形结构的枝节点) Component的实现子类 适用于: 单个对象和组合对象的使用具有一致性.将对象组

Extjs在form展示后台单个对象的属性

目的:写一个按钮事件,点击时弹出一个win窗体,里面镶嵌form表单,并且展示后台单个对象的属性 1 先来后台: 2 public void find(){ 3 String clientId = request.getParameter("clientId"); 4 BusinessContract bc = businessContractService.getLastContract(clientId); 5 response.setContentType("text/

12-自定义对象(单个对象)

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <script> //需求:单个自定义对象. //缺点:传单单个对象的时候还好,创建多个多线的时候变得非常繁琐 //创建多个对象,for循环不能修改名字的值.函数也可以创建多