一、定位方法

注意:元素属性必须唯一存在

#id定位
find_element_by_id()
#name定位
find_element_by_name()
#class_name定位
find_element_by_class_name()
#链接文本定位
find_element_by_link_text()
#部分链接文本定位
find_element_by_partial_link_text()
#xpath定位
find_element_by_xpath()
#css定位
find_element_by_css_selector()
#tag_name定位
find_element_by_tag_name()

#selenium By定位,使用这种用法前需要导入By类,如下:
from selenium.webdriver.common.by import By
#id定位
find_element(By.ID,value)
#name定位
find_element(By.NAME,value)
#class_name定位
find_element(By.CLASS_NAME,value)
#链接文本定位
find_element(By.LINK_TEXT,value)
#部分链接文本定位
find_element(By.PARTIAL_LINK_TEXT,value)
#xpath定位
find_element(By.XPATH,value)
#css定位
find_element(By.CSS_SELECTOR,value)
#tag_name定位
find_element(By.TAG_NAME,value)

1.id定位

#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_id("kw").send_keys("python")

2.name定位

#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_name("wd").send_keys("python")

3.class_name定位

#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_class_name("s_ipt").send_keys("python")

4.链接文本定位

#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_link_text("hao123").click()

5.部分链接文本定位

#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_partial_link_text("hao").click()

6.xpath定位

(1)直接复制页面中xpath

(2)相对路径(找祖籍)

  • 两个斜线(//)代表相对路径
  • 使用了通配符 * 表示匹配任意标签名
#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_xpath('//*[@class="s_ipt"]').send_keys("python")
#coding=utf-8

from selenium import webdriver
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
dr.find_element_by_xpath('//input[@class="s_ipt"]').send_keys("python")
#coding=utf-8

from selenium import webdriver
from time import sleep

dr =webdriver.Chrome()
dr.get("http://xxxxxxxx.com/")
dr.find_element_by_class_name("login").click()
title1 = dr.title
dr.find_element_by_xpath('//input[@placeholder="请输入手机号码" and @name="loginName"]').send_keys("1777982xxxx")
dr.find_element_by_name("loginPwd").send_keys("xxxxxxxx")
dr.find_element_by_xpath('//*[@class="login1"]/div[2]').click()
sleep(2)
title2 = dr.title
if title1 == title2:
    raise AssertionError
else:
    pass 

(3)绝对路径/层级定位

  • 从HTML元素DOM树从上至下一级一级查找
  • / 表示上下级
#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("http://192.168.0.161:8081/discuz/forum.php")
time.sleep(2)
dr.find_element_by_xpath("html/body/div[5]/div/div[1]/form/div/div/table/tbody/tr[1]/td[2]/input").send_keys("zhengying")

(4)相对路径结合绝对路径用法

#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("http://192.168.0.161:8081/discuz/forum.php")
time.sleep(2)
dr.find_element_by_xpath('//*[@id="lsform"]/div/div/table/tbody/tr[1]/td[2]/input').send_keys("zhengying")

7.css定位

css常用符号:

  • #  表示id
  • .  表示class
  • >  表示子元素,层级

(1)css中通过id定位

#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
time.sleep(2)
dr.find_element_by_css_selector("#kw").send_keys("zhengying")

(2)css中通过class定位

#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("https://www.baidu.com")
time.sleep(2)
dr.find_element_by_css_selector(".s_ipt").send_keys(u"新方硕")

(3)通过其它属性定位

find_element_by_css_selector("[属性=‘属性值’]")

find_element_by_css_selector("[属性=属性值]") ---------------- 请注意这里属性值没有加引号
#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("http://192.168.0.161:8081/discuz/forum.php")
time.sleep(2)
dr.find_element_by_css_selector('[value="请输入搜索内容"]').send_keys(u"新方硕")

(4)通过父子关系定位:

  • 假如你不知道某人的身份证号码、名字、手机号码等信息时无法取得联系,但是你知道某人爸爸的手机号码,此时你可以通过他爸爸来找到某人。
  • 换成术语就是当我们定位元素时,发现没有可以标识的唯一的元素的属性值时,那我们可以考虑用父亲标签结合属性来定位元素。
  • css路径定位时,不能和xpath定位一样标注出第几个标签
#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("http://192.168.0.161:8081/discuz/forum.php")
time.sleep(2)
dr.find_element_by_css_selector('.scbar_txt_td > input').send_keys(u"新方硕")

(5)css的绝对路径定位

#coding=utf-8

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get("http://192.168.0.161:8081/discuz/forum.php")
time.sleep(2)
dr.find_element_by_css_selector('html>body>div>div>div>form>div>div>table>tbody>tr>td>input').send_keys(u"新方硕")

python selenium八大定位方法的更多相关文章

  1. python selenium(定位方法)

    一.定位方法 注意:元素属性必须唯一存在 #id定位 find_element_by_id() #name定位 find_element_by_name() #class_name定位 find_el ...

  2. Python+Selenium自动化-定位一组元素,单选框、复选框的选中方法

    Python+Selenium自动化-定位一组元素,单选框.复选框的选中方法   之前学习了8种定位单个元素的方法,同时webdriver还提供了8种定位一组元素的方法.唯一区别就是在单词elemen ...

  3. Python+Selenium自动化-定位页面元素的八种方法

    Python+Selenium自动化-定位页面元素的八种方法   本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子. 0.元素定位方法主要有: id定位:find_elemen ...

  4. python之selenium元素定位方法

    前提: 大家好,今天我们来学习一下selenium,今天主要讲解selenium定位元素的方法,希望对大家有所帮助! 内容: 一,selenium定位元素 selenium提供了8种方法: 1.id ...

  5. python+selenium元素定位——8种方法

    定位元素,selenium提供了8中元素定位方法: (1)find_element_by_id() :html规定,id在html中必须是唯一的,有点类似于身份证号 (2)find_element_b ...

  6. python selenium 元素定位(三)

    上两篇的博文中介绍了python selenium的环境搭建和编写的第一个自动化测试脚本,从第二篇的例子中看出来再做UI级别的自动化测试的时候,有一个至关重要的因素,那就是元素的定位,只有从页面上找到 ...

  7. Python Appium 元素定位方法简单介绍

    Python  Appium  元素定位 常用的八种定位方法(与selenium通用) # id定位 driver.find_element_by_id() # name定位 driver.find_ ...

  8. selenium 之定位方法

    1 id 定位 driver.find_element_by_id() HTML 规定id 属性在HTML 文档中必须是唯一的.这类似于公民的身份证号,具有很强的唯一性 from selenium i ...

  9. Python&Selenium智能等待方法封装

    摘要:本篇博文用几行代码展示Python和Selenium做自动化测试时常见的显示等待和封装 # 用于实现智能等待页面元素的出现 # encoding = utf-8 ""&quo ...

随机推荐

  1. 如何通过配置tomcat或是web.xml让ie直接下载文件

    web.xml(tomcat\conf\web.xml)中配置了 <mime-mapping>   <extension>txt</extension>   < ...

  2. elasticsearch jestclient api

    1.es search sroll 可以遍历索引下所有数据 public class TestDemo { @Test public void searchSroll() { JestClientFa ...

  3. Spring整合Redis,并配置Jedis连接池

    目录 只言片语 创建redis连接池的配置文件 单机版 spring整合redis(使用JedisPool) 项目中使用示例 集群版 spring整合redis(使用JedisCluster) 项目中 ...

  4. OpenShift上的OpenvSwitch入门

    前段时间参加openshift培训,通过产品部门的讲解,刷新了我对OpenShift一些的认识,今天先从最弱的环节网络做一些了解吧. Openvswitch是openshift sdn的核心组件,进入 ...

  5. css3逐帧动画

    写css3动画的时候,我们经常用到animation来实现,默认情况下,animation是属于连贯性的ease动画.我们熟悉的animation动画有ease.ease-in.ease-out.li ...

  6. [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. [LeetCode] 802. Find Eventual Safe States 找到最终的安全状态

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  8. Eclipse安装Properties Editor插件

    安装步骤 1.打开eclispe编辑器help-->install new soft 2.输入软件地址 name:properties editor Location:http://proped ...

  9. C++标准异常与自定义异常

    参见https://www.runoob.com/cplusplus/cpp-exceptions-handling.html C++ 标准的异常 C++ 提供了一系列标准的异常,定义在 中,我们可以 ...

  10. Django框架之DRF get post put delete 使用简单示例 (利用序列化反序列化)

    路由配置 # 路由 from django.conf.urls import url from django.contrib import admin from app01 import views ...