Selenium所用版本为3.11.0
第一个Selenium自动化测试脚本
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("Selenium")
driver.find_element_by_id("su").click()
driver.quit()
WebDriver中的常用方法
点击
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
driver.quit()
提交
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
search_text=driver.find_element_by_id('kw')
search_text.send_keys('selenium')
search_text.submit()
driver.quit()
获取属性值等
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
#获得输入框的尺寸
size = driver.find_element_by_id('kw').size
print(size)
#返回百度页面底部备案信息
text=driver.find_element_by_id("bottom_layer").text
print(text)
#返回元素的属性值,可以是id、name、type或其他任意属性
attribute=driver.find_element_by_id("kw").get_attribute('type')
print(attribute)
#返回元素的结果是否可见,返回结果为True或False
result = driver.find_element_by_id("kw").is_displayed()
print(result)
driver.quit()
控制浏览器窗口大小
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://m.baidu.com")
#参数数字为像素
print("设置浏览器宽480、高800显示")
# driver.set_window_size(480,800)
driver.maximize_window()
driver.quit()
控制浏览器后退、前进
from selenium import webdriver
driver = webdriver.Chrome()
#访问百度首页
first_url = 'http://www.baidu.com'
print("now access %s" %(first_url))
driver.get(first_url)
#访问新闻页
second_url = 'http://news.baidu.com'
print("now access %s" %(second_url))
driver.get(second_url)
#返回(后退)到百度首页
print("back to %s " %(first_url))
driver.back()
#手动刷新
driver.refresh()
#前进到新闻页
print("forward to %s " %(second_url))
driver.forward()
driver.quit()
悬停
from selenium import webdriver
#引入ActionChains类
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.cn")
driver.maximize_window()
#定位到要悬停的元素
above=driver.find_element_by_link_text("设置")
#对定位到的元素执行鼠标悬停操作
ActionChains(driver).move_to_element(above).perform()
键盘操作
from selenium import webdriver
#调用key模块
from selenium.webdriver.common.keys import Keys
#在输入框输入内容
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
#删除多输入的一个m
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
#输入空格键+"教程"
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys("教程")
#输入组合键Ctrl+a,全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
#输入组合键Ctrl+x,剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
#输入组合键Ctrl+v,粘贴输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')
#用回车键代替单击操作
driver.find_element_by_id("su").send_keys(Keys.ENTERN)
driver.quit()
获得验证信息
from time import sleep
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
print("Before search=====================")
#打印当前页面title
title=driver.title
print("title:"+title)
#打印当前页面URL
now_url=driver.current_url
print("URL:"+now_url)
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
sleep(2)
print("After search========================")
#再次打印当前页面title
title=driver.title
print("title:"+title)
#再次打印当前页面URL
now_url=driver.current_url
print("URL:"+now_url)
#获取搜索结果条数
num = driver.find_element_by_class_name("nums").text
print("result:"+num)
driver.quit()
网友评论