搜罗了一些查找元素的除标准语句外,另外的语句使用方法,摘自 开源中国 郝云鹏
driver = webdriver.Chrome();
打开测试页面
driver.get( "http://baidu.com" );
---------------页面元素定位(页面元素)--------------
通过ID进行定位
<div id="coolestWidgetEvah">...</div> #页面代码
--->
element = driver.find_element_by_id("coolestWidgetEvah")

from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")#个人认为第二种比较麻烦! 通过Class Name定位
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>#页面代码
--->
cheeses = driver.find_elements_by_class_name("cheese")
或者
from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese") 通过Tag Name定位
<iframe src="..."></iframe>#页面代码
--->
frame = driver.find_element_by_tag_name("iframe")
或者
from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe") 通过Name定位
<input name="cheese" type="text"/>#页面代码
--->
cheese = driver.find_element_by_name("cheese")
或者
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese") 通过Partial Link Text(局部链接文本)定位
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>#页面代码
--->
cheese = driver.find_element_by_partial_link_text("cheese")
或者
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese") 通过CSS定位
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>#页面代码
--->
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")或者from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
#不建议使用此方法 因为在官网上提示 不通的浏览器中可能会导致显示不同的css样式
以上为基本的定位 还有一种xpath定位 下次再说.
-------------原文为selenium官方文档----------------
 

<自动化测试>之<selenium API 查找元素操作底层方法>的更多相关文章

  1. 彻底弄清c标准库中string.h里的常用函数用法

    在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...

  2. 走进C标准库(8)——"string.h"中函数的实现相关字符串操作函数

    我的strcat: char *strcat(char *dest,char *src) { char * reval = dest; while(*dest) dest++; while(*src) ...

  3. 走进C标准库(3)——"stdio.h"中的getc和ungetc

    接前文. 再来看看getc和ungetc的实现.在看这两个函数的实现之前,我们先来想一想这两个函数分别需要做的工作. int getc(FILE *stream) 说明:函数getc从stream指向 ...

  4. 走进C标准库(2)——"stdio.h"中的fopen函数

    其他的库文件看起来没有什么实现层面的知识可以探究的,所以,直接来看stdio.h. 1.茶余饭后的杂谈,有趣的历史 在过去的几十年中,独立于设备的输入输出模型得到了飞速的发展,标准C从这个改善的模型中 ...

  5. 走进C标准库(1)——assert.h,ctype.h

    默默觉得原来的阅读笔记的名字太土了,改了个名字,叫做走进C标准库. 自己就是菜鸟一只,第一次具体看C标准库,文章参杂了对<the standard C library>的阅读和对源码的一些 ...

  6. C 非标准库(conio.h)

    所谓的 C 标准库(C standard library),是指在 ISO C 或者 POSIX 标准中定义的: POSIX is a superset(超集) of the standard C l ...

  7. 走进C标准库(4)——"stdio.h"中的putc

    花了点时间把园子弄得好看了点,现在继续. 函数名: putc 功  能: 输出一字符到指定流中 用  法: int putc(int ch, FILE *stream); #define _putc_ ...

  8. 走进C标准库(5)——"stdio.h"中的其他部分函数

    函数介绍来自:http://ganquan.info/standard-c/ 函数名: freopen 功  能: 替换一个流 用  法: FILE *freopen(char *filename, ...

  9. 走进C标准库(6)——"string.h"中函数的实现memchr

    我写的memchr: void *memchr(const void *buf, char ch, unsigned count){ unsigned ; while(*(buf++) != ch & ...

  10. 走进C标准库(7)——"string.h"中函数的实现memcmp,memcpy,memmove,memset

    我的memcmp: int memcmp(void *buf1, void *buf2, unsigned int count){ int reval; while(count && ...

随机推荐

  1. python中实现查找字符串的find函数

    第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2为要查找的字符串 def index_of_s ...

  2. 第二章(1.3)Python基础知识(输入输出)

    一.?输出 用print加上字符串,就可以向屏幕上输出指定的文字 print?'hello, world' print也可以打印整数. >>> print?300 二.?输入 Pyt ...

  3. Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class

    Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class A well-architecte ...

  4. php7.0 新增运算符??

    ??是php7 新增符号 其作用近似于三目运算符 ?: 但存在着细微差别 比较示例代码如图:         $b = $a?$a:2; 三目运算 <=>     $e = $a??'ho ...

  5. 用倍增法构造后缀数组中的SA及RANK数组

    感觉后缀数组很难学的说= = 不过总算是啃下来了 首先 我们需要理解一下倍增法构造的原理 设原串的长度为n 对于每个子串 我们将它用'\0'补成长度为2^k的串(2^k-1<n<=2^k) ...

  6. obj文件中的关键字

    obj文件使用的关键字 关键字 含义 v 表示本行指定一个顶点,此关键字后跟着3个单精度浮点数,分别表示该顶点的X.Y.Z坐标值 vt 表示本行指定一个纹理坐标,此关键字后跟着两个单精度浮点数,分别表 ...

  7. ubuntu+qt+opencv

    linux下Qt+OpenCv环境的搭建: https://blog.csdn.net/yaowangII/article/details/84303124 1.qt:https://blog.csd ...

  8. VS2012修改代码时会把后面的覆盖

    vs2012修改代码时会把后面的覆盖,并且鼠标指针变成灰色竖方块 解决:按一下键盘上的Insert键

  9. with cats as pets get cataracts and macular degeneration

    I really enjoyed this talk, optimistic and helpful. May I offer a small but perhaps helpful bit of k ...

  10. Python爬虫抓取 python tutorial中文版,保存为word

    看到了中文版的python tutorial,发现是网页版的,刚好最近在学习爬虫,想着不如抓取到本地 首先是网页的内容 查看网页源码后发现可以使用BeautifulSoup来获取文档的标题和内容,并保 ...