博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
python+selenium进行简单验证码获取
查看>>
where,having与 group by连用的区别
查看>>
【MySQL】MySQL锁和隔离级别浅析二 之 INSERT
查看>>
Oracle T4-2 使用ILOM CLI升级Firmware
查看>>
4.14上午
查看>>
数据分析 -- 白话一下什么是决策树模型(转载)
查看>>
Java SPI机制原理和使用场景
查看>>
web前端java script学习2017.7.18
查看>>
删除TXPlatform
查看>>
LaTex:图片排版
查看>>
并发访问超时的问题可能性(引用)
查看>>
中小团队基于Docker的Devops实践
查看>>
利用python打开摄像头并保存
查看>>
System函数的使用说明
查看>>
Selenium-测试对象操作之:获取浏览器滚动条滚动距离
查看>>
Linux下MySQL数据库安装与配置
查看>>
Extjs String转Json
查看>>
oracle入门(4)——少而常用的命令
查看>>
打印机设置(PrintDialog)、页面设置(PageSetupDialog) 及 RDLC报表如何选择指定打印机...
查看>>
Java 虚拟机部分面试题
查看>>