∮定位单个元素
打车去目的地需要告诉司机地名或者周围标志建筑==,那么我们要让机器去运行Web,就需要告诉Web元素对应的特征。
接下来~~~算了,还是以我们万能的度娘为例ο(=•ω<=)ρ⌒☆
例子 | 属性 | 属性值 | 定位代码 | ||
Java | Python | Ruby | |||
搜索框 | id | kw | - | find_element_by_id("kw") | find_element(:id,'kw') |
class | s_ipt | - | find_element_by_class_name("s_ipt") | find_element(:class,'s_ipt') | |
name | wd | - | find_element_by_name("wd") | find_element(:name,'wd') | |
tag | input | - | find_element_by_tag_name("input") | find_element(:tag_name,'input') | |
文字链接 | 标签a链接 | hao123 | - | find_element_by_link_text("hao123") | find_element(:link,'hao123') |
标签a链接 | hao123 | - | find_element_by_partial_link_text("hao") | find_element(:partial_link_text,'hao') | |
XPath | id | kw | - | find_element_by_xpath("//*[@id='kw']") | find_element(:xpath,"//*[@id='kw']") |
name | wd | - | find_element_by_xpath("//*[@name='wd']") | find_element(:xpath,"//*[@name='wd']") | |
class | s_ipt | - | find_element_by_xpath("//*[@class='s_ipt']") | find_element(:xpath,"//*[@class='s_ipt']") | |
maxlength | 100 | - | find_element_by_xpath("//*[@maxlength='100']") | find_element(:xpath,"//*[@maxlength='100']") | |
autocomplete | off | - | find_element_by_xpath("//*[@autocpmplete='off']") | find_element(:xpath,"//*[@autocpmplete='off']") | |
type | submit | - | find_element_by_xpath("//*[@type='submit']") | find_element(:xpath,"//*[@type='submit']") | |
CSS | id | kw | - | find_element_by_css_selector("#kw") | find_element(:css,'#kw') |
name | wd | - | find_element_by_css_selector("[name='wd']") | find_element(:css,"[name='wd']") | |
class | s_ipt | - | find_element_by_css_selector(".s_ipt") | find_element(:css,'.s_ipt') | |
autocomplete | off | - | find_element_by_css_selector("[autocomplete=off]") | find_element(:css,"[autocomplete=off]") | |
type | submit | - | find_element_by_css_selector("[type='submit']") | find_element(:css,"[type='submit']") | |
By | id | kw | findElement(By.id("kw")) | find_element(By.ID,"kw") | - |
name | wd | findElement(By.name("wd")) | find_element(By.NAME,"wd") | - | |
class | s_ipt | findElement(By.className("s_ipt")) | find_element(By.CLASS_NAME,"s_ipt") | - | |
html | input | findElement(By.tagName("input")) | find_element(By.TAG_NAME,"input") | - | |
标签a链接 | hao123 | findElement(By.linkText("hao123")) | find_element(By.LINK_TEXT,"hao123") | - | |
标签a链接 | hao123 | findElement(By.partialLinkText("hao")) | find_element(By.PARTIAL_LINK_TEXT,"hao") | - | |
XPath | //*[@class='s_ipt'] | findElement(By.xpath("//*[@class='s_ipt']")) | find_element(By.XPATH,"//*[@class='s_ipt']") | - | |
CSS | span[@class='bg s_btn_wr']/input[@id='su'] | findElement(By.cssSelector("span.bg s_btn_wr>input#su")) | find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su") | - | |
↑ 以上部分将通过某个元素的特征来定位元素的(如有遗漏或差错各位可自行补充修改) 要是像那种没啥特色的“孩子”,我们还是通过它们的父辈来找到它们 ↓ “我的祖宗是body!”( ఠൠఠ )ノ“啪~哪来的熊孩子?我还是html呢~” |
|||||
XPath | /html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input /*通过绝对路径找到百度输入框*/ | ||||
/html/body/div[1]/div[1]/div/div[1]/div/form/span[2]/input /*通过绝对路径找到百度搜索按钮*/ | |||||
//span[@class='bg s_ipt_wr']/input /*通过层级与属性结合找到百度输入框*/ | |||||
//form[@id='form']/span[2]/input /*通过层级与属性结合找到百度搜索按钮*/ | |||||
//input[@id='kw' and @class='su']/span/input /*通过逻辑运算符获取元素,通过某个元素多个特征找到元素*/ | |||||
CSS | form#form>span>input#kw(CSS不过多给出案例,想要了解更多可自行去查找资料(/ω\*)……… (/ω•\*)) | ||||
( ̄y▽ ̄)╭ Ohohoho.....虽然获取元素的方法有很多,但是较常用的还是XPath。获取XPath的途径有俩种:☛ 无敌的我,你们都是渣渣_____(ˉ▽ ̄~) 切~~☛ 有那个时间不如多撸根肉串,旧版Firefox的FireBug(新版Firefox自带获取xpath),Ctrl C/Ctrl V一下(o゜▽゜)o☆ |
**代码时间 **
实践出真知。列了这么多方法,还是有必要检测一下是否有效,虽然无法全部验证,但是还是要往下看往下看往下看☟☟☟
Java
Python
Ruby
∰定位一组元素
定义一组元素的方法与定位单个元素的方法类似,唯一的区别在单词"element"是否加了"s"。元素组的一般操作场景:
- 批量操作元素(如全选)
- 选择一组元素中所需要的部分(如全选后,取消不需要的部分)
Java | Python | Ruby | |
id | driver.findElements(By.id,) | find_elements_by_id | find_elements(:id,) |
name | driver.findElements(By.name,) | find_elements_by_name | find_elements(:name,) |
class | driver.findElements(By.className,) | find_elements_by_class_name | find_elements(:class,) |
tag | driver.findElements(By.tagName,) | find_elements_by_tag_name | find_elements(:tag_name,) |
link | driver.findElements(By.linkText,) | find_elements_by_link_text | find_elements(:link,) |
partial_link | driver.findElements(By.partialLinkText,) | find_elements_by_partial_link_text | find_elements(:partial_link_text,) |
xpath | driver.findElements(By.xpath,) | find_elements_by_xpath | find_elements(:xpath,) |
css | driver.findElements(By.cssSelector,) | find_elements_by_css_selector | find_elements(:css,) |
在这里只简简单单罗列出了8大种定位一组元素的方法,但实际上可以参考定位单个元素的方法上进行扩展,在这就不展现完整了(记得加's'!!!) |
有话要说
自动化不仅适用于web端,也可以适用于手机端,虽说它们之间调用的包呀,或者说是定位元素方法不太一样,但是它们定位元素的介质是一样的,如果要是你写的代码定位不到元素,那就要检查你的元素介质是否是对的,尤其是用绝对路径定位的时候呦ヾ( ̄▽ ̄)ByeBye