Detailed explanation of Selenium element positioning and WebDriver common methods

  • 2021-08-12 03:03:41
  • OfStack

1. 8 Ways to Position Elements

1. Method introduction

定位1个元素 定位多个元素 含义
find_element_by_id() find_elements_by_id() 通过元素id定位
find_element_by_name() find_elements_by_name() 通过元素name定位
find_element_by_xpath() find_elements_by_xpath() 通过xpath表达式定位
find_element_by_link_text() find_elements_by_link_text() 通过完整超链接定位
find_element_by_partial_link_text() find_elements_by_partial_link_text() 通过部分链接定位
find_element_by_tag_name() find_elements_by_tag_name() 通过标签定位
find_element_by_class_name() find_elements_by_class_name() 通过类名进行定位
find_elements_by_css_selector() find_elements_by_css_selector()

2. Example demonstration


from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')

# Through elements id Positioning () 
driver.find_element_by_id('kw')

# Through elements name Positioning 
driver.find_element_by_name('wd')

# Locate by class name 
driver.find_element_by_class_name('s_ipt')

# Locate by label 
driver.find_element_by_tag_name('input')

# Pass xpath Expression positioning 
driver.find_element_by_xpath('//*[@id="kw"]')

# Pass css Selector for positioning 
driver.find_element_by_css_selector('#kw')

# Locate through full hyperlinks 
driver.find_element_by_link_text(' News ')

# Locate through partial links 
driver.find_element_by_partial_link_text('hao')

driver.quit()# Close all tabs 

The positioning of xpaht and css is complicated, please refer to:

XPath acquisition method
XPath Syntax
CSS selector syntax

Locating here may not directly view the effect (the printed result is the obtained element object) Positioning 1 is generally used in conjunction with 1 common methods In the above examples, it is a single element positioning. Please refer to the above method introduction for multiple element positioning keywords

2. Common methods of WebDriver (used in conjunction with positioning methods)

1. Click and Enter

clear (): Clear text, mostly for input boxes send_keys (): Analog key input, mostly for input boxes click (): Click on an element for a wide range of uses

For more mouse and keyboard events, please refer to:
Python crawler-Selenium (5) mouse event
Python Crawler-Selenium (6) Keyboard Events


from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get('https://www.baidu.com/')

driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(" Miscellaneous Notes on Cheng Xuyuan ")
driver.find_element_by_id("su").click()
time.sleep(5)

driver.quit() #  Close all tabs 

3. Submit

submit (): Used to submit forms, equivalent to carriage return, and far less widely used than click ()


from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get('https://www.baidu.com/')

driver_id = driver.find_element_by_id("kw")
driver_id.send_keys(" Miscellaneous Notes on Cheng Xuyuan ")
driver_id.submit()
time.sleep(5)

driver.quit() #  Close all tabs 

STEP 4 Get something

title: Get the title of the current page current_url: The user gets the URL of the current page size: Get the size of the element text: Get the text of the element get_attribute (): Get the property value is_displayed (): Is the element user visible

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('https://www.baidu.com/')

#  Object for the current page title
title = driver.title
print(title)

# Object for the current page url
url = driver.current_url
print(url)

#  Get the size of the input box 
input_size = driver.find_element_by_id('kw').size
print(input_size)

#  Return to the bottom of Baidu page for filing information 
text = driver.find_element_by_id("cp").text
print(text)

#  Returns the attribute value of the element,   It can be  id ,  name ,  type  Or any other property 
attribute = driver.find_element_by_id("kw").get_attribute('type')
print(attribute)

#  Returns whether the result of the element is visible,   The result returned is  True  Or  False
result = driver.find_element_by_id("kw").is_displayed()
print(result)

driver.quit() #  Close all tabs 


Selenium Collection Portal:

标题 简介
Python爬虫 - Selenium(1)安装和简单使用 详细介绍Selenium的依赖环境在Windows和Centos7上的安装及简单使用
Python爬虫 - Selenium(2)元素定位和WebDriver常用方法 详细介绍定位元素的8种方式并配合点击和输入、提交、获取断言信息等方法的使用
Python爬虫 - Selenium(3)控制浏览器的常用方法 详细介绍自定义浏览器窗口大小或全屏、控制浏览器后退、前进、刷新浏览器等方法的使用
Python爬虫 - Selenium(4)配置启动项参数 详细介绍Selenium启动项参数的配置,其中包括无界面模式、浏览器窗口大小设置、浏览器User-Agent (请求头)等等
Python爬虫 - Selenium(5)鼠标事件 详细介绍鼠标右击、双击、拖动、鼠标悬停等方法的使用
Python爬虫 - Selenium(6)键盘事件 详细介绍键盘的操作,几乎包含所有常用按键以及组合键
Python爬虫 - Selenium(7)多窗口切换 详细介绍Selenium是如何实现在不同的窗口之间自由切换
Python爬虫 - Selenium(8)frame/iframe表单嵌套页面 详细介绍如何从当前定位的主体切换为frame/iframe表单的内嵌页面中
Python爬虫 - Selenium(9)警告框(弹窗)处理 详细介绍如何定位并处理多类警告弹窗
Python爬虫 - Selenium(10)下拉框处理 详细介绍如何灵活的定位并处理下拉框
Python爬虫 - Selenium(11)文件上传 详细介绍如何优雅的通过send_keys()指定文件进行上传
Python爬虫 - Selenium(12)获取登录Cookies,并添加Cookies自动登录 详细介绍如何获取Cookies和使用Cookies进行自动登录
Python爬虫 - Selenium(13)设置元素等待 详细介绍如何优雅的设置元素等待时间,防止程序运行过快而导致元素定位失败
Python爬虫 - Selenium(14)窗口截图 详细介绍如何使用窗口截图
Python爬虫 - Selenium(15)关闭浏览器 详细介绍两种关闭窗口的区别


Related articles: