1.利用ID定位元素(能直接通过ID来定位的元素比较少)

# coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
url = ("http://www.baidu.com")
driver.get(url)
try:
    driver.find_element_by_id("kw")
    print('test pass: ID found')
except Exception as e:
    print("Exception found",format(e))
driver.quit()

2.利用tag name定位元素(tag name重复的比较多,定位不够精确)

driver.find_element_by_tag_name("form")

3.利用link text定位元素

driver.find_element_by_link_text("新闻")

4.利用partial link text定位元素

driver.find_element_by_pertial_link_text("主页").click()

5.利用class name定位元素

driver.find_element_by_class_name("s_ipt")

6.利用name定位元素(不是所有节点都有这个属性)

driver.find_element_by_name("wd")

7.利用css定位元素

driver.find_element_by_css_selector("#su")

8.利用xpath定位元素

driver.find_element_by_xpath("//*[@id='kw'].send_keys("selenium")

#driver.find_element_by_xpath("//*[@id='kw']/a[text()='新闻']")

#driver.find_element_by_xpath("//*[a[contains(@href,'diannao')]     #contains方法

driver.find_element_by_xpath("//div/h3/a[text()='官网']/../a/em[text()='Selenium']").is_displayed()  #相对路径

python + selenium 练习篇 - 定位元素的方法的更多相关文章

  1. Python+Selenium定位元素的方法

    Python+Selenium有以下八种定位元素的方法: 1. find_element_by_id() eg: find_element_by_id("kw") 2. find_ ...

  2. python+selenium基础篇,By定位元素

    1.By定位和find_element_by_XXXXXX是一样的,如下图所示,定位元素的方法都是一样的 2.使用By定位代码如下所示 from selenium import webdriver f ...

  3. Python selenium根据class定位页面元素

    在日常的网页源码中,我们基于元素的id去定位是最万无一失的,id在单个页面中是不会重复的.但是实际工作中,很多前端开发人员并未给每个元素都编写id属性.通常一段html代码如下: <div cl ...

  4. Python+Selenium练习篇之1-摘取网页上全部邮箱

    前面已经介绍了Python+Selenium基础篇,通过前面几篇文章的介绍和练习,Selenium+Python的webUI自动化测试算是入门了.接下来,我计划写第二个系列:练习篇,通过一些练习,了解 ...

  5. Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》

    Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676

  6. python去除列表中重复元素的方法

    列表中元素位置的索引用的是L.index 本文实例讲述了Python去除列表中重复元素的方法.分享给大家供大家参考.具体如下: 比较容易记忆的是用内置的set 1 2 3 l1 = ['b','c', ...

  7. 【Selenium01篇】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 二.话不多说,直接开干,开始搭建自动化测试环境 这里以前在 ...

  8. Python+Selenium练习篇之2-利用ID定位元素

    在前面一篇文章,我们介绍了如何摘取页面字段,通过正则进行匹配符合要求的字段.如果感觉有点困难,不能立马理解,没有关系.把字符串摘取放到第一篇,是因为自动化测试脚本,经常要利用字符串操作,字符串切割,查 ...

  9. Python+Selenium练习篇之8-利用css定位元素

    前面介绍了,XPath, id , class , link text, partial link text, tag name, name 七大元素定位方法,本文介绍webdriver支持的最后一个 ...

随机推荐

  1. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

  2. springMVC:modelandview,model,controller,参数传递

    转载:http://blog.csdn.net/wm5920/article/details/8173480 1.web.xml 配置: copy   <> ></> & ...

  3. GridView里的按钮事件

    http://www.cnblogs.com/insus/archive/2012/09/22/2697862.html using System;using System.Collections.G ...

  4. winform改变控件的外形

    GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 0, 40, 40); Region region = new Region(gp); c ...

  5. requireJS入门基础

    参考  require.js详解 1.引用requireJS的html文件 <!DOCTYPE html> <head> <title>requireJS</ ...

  6. (字符转化)UTF-8和GBK有什么区别?

    GBK包含全部中文字符:UTF-8则包含全世界所有国家需要用到的字符. GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字 ...

  7. atcoder/CODE FESTIVAL 2017 qual B/B(dfs染色判断是否为二分图)

    题目链接:http://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_c 题意:给出一个含 n ...

  8. Linux的SSH(Secure Shell Protocol)服务

    在数据传输前,SSH会对需要传输的数据进行加密,保证会话安全与会话中传输数据的安全,SSH客户端还包含一个远程拷贝scp. 1.SSH的结构 SSH服务由服务端软件(openssh)和客户端(SSH. ...

  9. Spring注解实现定时功能以及Quartz定时器

    一:Spring注解实现--------->Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz 1:maven配置: <!-- quartz--> <d ...

  10. Oracle11g 脚本创建表空间和用户

    /*分为四步 *//*第1步:创建临时表空间 */create temporary tablespace emaoyi_temp tempfile 'D:\app\Administrator\prod ...