博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenium Locating Elements
阅读量:5013 次
发布时间:2019-06-12

本文共 2678 字,大约阅读时间需要 8 分钟。

Locating Elements

Location Methods:

find_element_by_idfind_element_by_namefind_element_by_xpathfind_element_by_link_textfind_element_by_partial_link_textfind_element_by_tag_namefind_element_by_class_namefind_element_by_css_selectorfind_elements_by_namefind_elements_by_xpathfind_elements_by_link_textfind_elements_by_partial_link_textfind_elements_by_tag_namefind_elements_by_class_namefind_elements_by_css_selector

Example usage:

from selenium.webdriver.common.by import Bydriver.find_element(By.XPATH, '//button[text()="Some text"]')driver.find_elements(By.XPATH, '//button')

By class attributes:

ID = "id"XPATH = "xpath"LINK_TEXT = "link text"PARTIAL_LINK_TEXT = "partial link text"NAME = "name"TAG_NAME = "tag name"CLASS_NAME = "class name"CSS_SELECTOR = "css selector"

1.Locating by Id

Page source:

   
login_form = driver.find_element_by_id('loginForm')

2.Locating by Name

Page source:

   
username = driver.find_element_by_name('username')password = driver.find_element_by_name('password')continue = driver.find_element_by_name('continue') //"Login" button,it occures before the "Clear" button

3.Locating by XPath

Page source:

   
//locating formlogin_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']")//locating usernameusername = driver.find_element_by_xpath("//form[input/@name='username']")username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")username = driver.find_element_by_xpath("//input[@name='username']")//locating "Clear"clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")

Page source:

   

Are you sure you want to do this?

Continue Cancelcontinue_link = driver.find_element_by_link_text('Continue')continue_link = driver.find_element_by_partial_link_text('Conti')

5.Locating Elements by Tag Name

Page source:

   

Welcome

Site content goes here.

heading1 = driver.find_element_by_tag_name('h1')

6.Locating Elements by Class Name

Page source:

   

Site content goes here.

content = driver.find_element_by_class_name('content') //Locating "p" element

7.Locating Elements by CSS Selectors

Page source:

   

Site content goes here.

content = driver.find_element_by_css_selector('p.content') Locating "p" element

节选自Selenium官网Doc:

https://selenium-python.readthedocs.io/locating-elements.html

转载于:https://www.cnblogs.com/music378/p/10481255.html

你可能感兴趣的文章
循环队列的运用---求K阶斐波那契序列
查看>>
pta 编程题14 Huffman Codes
查看>>
初始化bootstrap treeview树节点
查看>>
python selenium向<sapn>标签中写入内容
查看>>
JS常用坐标
查看>>
使用”结构化的思考方式“来编码和使用”流程化的思考方式“来编码,孰优孰劣?...
查看>>
C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)【转】...
查看>>
关于git的认证方式
查看>>
字符串按照字典序排列
查看>>
IOS 开发调用打电话,发短信
查看>>
CI 框架中的日志处理 以及 404异常处理
查看>>
keepalived介绍
查看>>
css3 标签 background-size
查看>>
python itertools
查看>>
Linux内核调试技术——jprobe使用与实现
查看>>
样式、格式布局
查看>>
ubuntu设计文件权限
查看>>
Vue双向绑定原理详解
查看>>
Android基础总结(5)——数据存储,持久化技术
查看>>
关于DataSet事务处理以及SqlDataAdapter四种用法
查看>>