1 id定位
id定位用于选择指定的网页中标签的id属性。由于网页中标签的id值是唯一的,因此不存在根据id定位选择多个元素的情况。以百度首页为例,百度搜索框id为kw
:
1.1使用find_element_by_id('标签id')
获取元素
import unittest
from selenium.webdriver.firefox import webdriver
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com')
elem = driver.find_element_by_id('kw') # 选择搜索框元素
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
1.2 使用find_element(By.ID, '标签id')
获取元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com')
elem = driver.find_element(By.ID, 'kw')
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
1.3 执行效果
2 class定位
class定位用于选择指定的网页中标签的class属性。由于网页中标签的class的值不是唯一的,因此可能存在根据s_ipt定位选择多个元素的情况。仍以百度为例,百度搜索框class
为s_ipt
。
2.1使用find_element_by_class_name('标签class')
获取元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com')
elem = driver.find_element_by_class_name('s_ipt')
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
2.1使用find_element(By.CLASS_NAME, '标签class')
获取元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com')
elem = driver.find_element(By.CLASS_NAME, 's_ipt')
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
3 link_text定位
link_text定位是以超链接全部文本来定位元素,以网易的 新闻
链接为例
3.1 通过find_element_by_link_text('链接文本')
获取链接元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.163.com/')
elem = driver.find_element_by_link_text('新闻')
print(elem.text)
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
3.2 通过find_element(By.LINK_TEXT, '链接文本')
获取链接元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.163.com/')
elem = driver.find_element(By.LINK_TEXT, '新闻')
print(elem.text)
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
4 partial_link_text定位
partial_link_text定位用于以链接中的部分文字为关键字来定位到该链接。以网易首页中以下新闻链接为例:
4.1 通过find_element_by_partial_link_text('链接中的文本')
来定位链接
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.163.com/')
elem = driver.find_element_by_partial_link_text('孝心饺子宴')
print(elem.text)
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
4.2 通过find_element(By.PARTIAL_LINK_TEXT, '链接中的文本')
来定位链接
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.163.com/')
elem = driver.find_element(By.PARTIAL_LINK_TEXT, '孝心饺子宴')
print(elem.text)
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
5 tag_name定位
tag_name定位即通过标签名称定位,以搜狗搜索为例,来获取form标签的action
属性。
5.1 获取属性代码
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.sogou.com/')
elem = driver.find_element_by_tag_name('form')
print(elem.get_attribute('action'))
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
5.2 执行输出
D:\Code\Python\easyflask\venv\Scripts\python.exe D:/Code/Python/easyflask/index.py
.
https://www.sogou.com/web
----------------------------------------------------------------------
Ran 1 test in 9.077s
OK
Process finished with exit code 0
6 name定位
name定位即通过标签的name属性来定位元素。以百度为例,获取输入框。
6.1 通过name定位元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com/')
elem = driver.find_element_by_name('wd')
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
7 css定位
css定位即通过css选择器来定位元素。仍以获取百度输入框为例
7.1 通过css定位元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com/')
elem = driver.find_element_by_css_selector('#kw')
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()
8 通过xpath定位
xpath定位来源于xml,又由于html语言的语法和xml比较接近,因此xpath也支持定位html页面元素。
通过调试模式复制元素的xpath路径。
8.1 通过xpath定位元素
import unittest
from selenium.webdriver.firefox import webdriver
from selenium.webdriver.common.by import By
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.WebDriver()
def test_search(self):
driver = self.driver
driver.get('https://www.baidu.com/')
elem = driver.find_element_by_xpath('//*[@id="kw"]')
elem.send_keys('selenium')
def tearDown(self):
# self.driver.close()
pass
if __name__ == '__main__':
unittest.main()