Selenium是一款功能强大的自动化测试框架,它可以模拟用户在浏览器中的各种操作,比如点击、输入等,通过自动化测试帮助开发人员减少手动测试的时间和成本,提高测试效率和代码质量。
本篇文章将从选取元素、常见操作、等待元素、多窗口处理和自定义浏览器驱动等多个方面详细阐述Selenium的使用方法。
一、选取元素
1、选取ID元素
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_id('su')
element.click()
driver.quit()
2、选取class元素
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_class_name('s_btn')
element.click()
driver.quit()
3、选取name元素
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_name('wd')
element.send_keys('selenium')
driver.quit()
4、选取link_text元素
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_link_text('新闻')
element.click()
driver.quit()
二、常见操作
1、输入文字
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_name('wd')
element.send_keys('selenium')
element.submit()
driver.quit()
2、点击按钮
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_id('su')
element.click()
driver.quit()
3、切换窗口
from selenium import webdriver
driver = webdriver.Chrome()
url1 = 'https://www.baidu.com/'
url2 = 'http://news.baidu.com/'
driver.get(url1)
driver.find_element_by_link_text('新闻').click()
handles = driver.window_handles
for handle in handles:
if handle != driver.current_window_handle:
driver.switch_to.window(handle)
print(driver.title)
driver.quit()
三、等待元素
1、强制等待
from selenium import webdriver
import time
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
time.sleep(5) # 等待5秒
driver.quit()
2、隐式等待
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.implicitly_wait(10) # 最长等待10秒
driver.get(url)
element = driver.find_element_by_name('wd')
element.send_keys('selenium')
element.submit()
driver.quit()
3、显式等待
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, 'wd')))
element.send_keys('selenium')
element.submit()
driver.quit()
四、多窗口处理
from selenium import webdriver
driver = webdriver.Chrome()
url1 = 'https://www.baidu.com/'
url2 = 'http://news.baidu.com/'
driver.get(url1)
driver.find_element_by_link_text('新闻').click()
handles = driver.window_handles
for handle in handles:
if handle != driver.current_window_handle:
driver.switch_to.window(handle)
driver.close()
driver.switch_to.window(handles[0])
driver.find_element_by_name('wd').send_keys('selenium')
driver.find_element_by_id('su').click()
driver.quit()
五、自定义浏览器驱动
1、火狐浏览器
from selenium import webdriver
firefox_options = webdriver.FirefoxOptions()
firefox_options.set_headless()
firefox_options.add_argument('--disable-gpu')
firefox_options.add_argument('--no-sandbox')
driver = webdriver.Firefox(firefox_options=firefox_options)
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_name('wd')
element.send_keys('selenium')
element.submit()
driver.quit()
2、Edge浏览器
from selenium import webdriver
from msedge.selenium_tools import EdgeOptions, Edge
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.headless = True
driver = Edge(options=edge_options)
url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_name('wd')
element.send_keys('selenium')
element.submit()
driver.quit()
通过以上的示例,您可以快速掌握Selenium的基本使用方法,希望能对您的自动化测试工作有所帮助。