Selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE,Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。 -- 百度百科
首先下载驱动文件:https://chromedriver.storage.googleapis.com/index.html?path=2.39/
放入google目录下
测试代码,测试是否能读取到驱动文件。
from selenium import webdriver path = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe" driver = webdriver.Chrome(executable_path=path) url = "https://www.baidu.com" driver.get(url) print(driver.page_source)
简单的实现浏览器测试
# -*- coding:utf-8 -*- from selenium import webdriver WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe" driver = webdriver.Chrome(executable_path=WebPath) driver.set_window_size(1000,500) url = "https://www.baidu.com" driver.get(url) print(driver.find_element_by_id("kw"))
Selenium 自动化测试库的使用:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="gbk"> <title>Selenium Test</title> </head> <body> <div class="acount" id="aid"> <a class="mnav" href="https://news.baidu.com" rel="external nofollow" name="trnews">新闻</a> <a class="mnav" href="https://lyshark.cnblogs.com" rel="external nofollow" name="myblog">我的博客</a> <a class="mnav" href="https://github.com/lyshark" rel="external nofollow" name="mygit">GitHub</a> </div> <form id="forms" class="fms" name="submit_form" action="index.html"> <span class="soutu-btn"></span> <p>用户: <input id="user" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off"></p> <p>密码: <input id="pass" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off"></p> <input type="submit" value="http://www.cppcns.com/jiaoben/python/提交" /> </form> <p name="p1" > hello lyshark p1</p> <p name="p2" > hello lyshark p2</p> </body> </html>
通过简单的浏览文件并实现简单的定位.
# 驱动下载地址: http://chromedriver.storage.googleapis.com/index.html from selenium import webdriver WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe" driver = webdriver.Chrome(executable_path=WebPath) driver.set_window_size(1024,768) # 常用的定位变量参数如下所示. driver.get("http://lyshark.com") print("当前URL: {}".format(driver.current_url)) print("当前标题: {}".format(driver.title)) print("网页代码: {}".format(driver.page_source)) # 基本的 find_element 标签查找定位方式 print(driver.find_element_by_id("user")) # 通过ID来查找元素 print(driver.find_element_by_name("p1").text) # 通过name属性来定位 print(driver.find_element_by_class_name("s_ipt")) # 通过类名来定位 # 通过xpath定位,xpath定位有N种写法,这里列几个常用写法 print(driver.find_element_by_xpath("//form[@class='fms']//input[@id='user']")) print(driver.find_element_by_xpath("//p[@name='p1']")) print(driver.find_element_by_xpath("//html/body/form/p/input")) print(driver.find_elements_by_css_selector(".fms #user")) # 定位a标签中的关键字. print(driver.find_element_by_link_text("新闻")) print(driver.find_element_by_partial_link_text("我"))
通过xpath定位标签并自动输入内容,发送登录请求到后端,写法如下.
from selenium import webdriver WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe" driver = webdriver.Chrome(executable_path=WebPath) driver.set_window_size(1024,768) driver.get("http://lyshark.com") # 通过xpath语法定位到用户名的标签上并且自动输入lyshark这个用户名 driver.find_element_by_xpath("//form[@class='fms']/p//input[@id='user']").send_keys("lyshark") # 通过xpath语法定位到密码的标签上清空默认值,然后输入123123密码 driver.find_element_by_xpath("//form[@class='fms']/p//input[@id='pass']").clear() driver.find_element_by_xpath("//form[@class='fms']/p//input[@id='pass']").send_keys("123123") # 提交这个请求,默认有两种提交方式一种是 click() 一种是submit() driver.find_element_by_xpath("//form[@class='fms']/input[@type='submit']").click()python Selenium 库的使用技巧
扫一扫手机访问
