重拾python之selenium(一)
一.初识Selenium
1.Selenium是用于web应用程序自动化测试的工具,支持众多浏览器Chrome,Firefox等主流浏览器
2.使用Selenium时需要准备:
- 浏览器是必须的,建议用Chrome、Firefox
- python,请用python3
- 安装selenium也是必须的(pip install selenium)
- Selenium3调用浏览器必须有一个webdriver驱动文件,所以在下载驱动时尽量下载最新的,在下载浏览器时要尽量使用旧一点的版本,毕竟webdriver可以向下兼容,诶.....让我去找找有没有webdriver的下载地址,copy,copy过来:
(Chrome)ChromeDriver:https://npm.taobao.org/mirrors/chromedriver
(Firefox)Geckodriver:https://github.com/mozilla/geckodriver/releases
selenium浏览器驱动网址:http://www.testclass.net/selenium_python/selenium3-browser-driver/,只是好东西!
二.Selenium入门
前面的准备工作完成后,我们可以先试试使用python导入selenium来控制浏览器访问网站,本文使用的是python3.8
from selenium import webdriver import time driver = webdriver.Chrome() # 实例化对象 driver.get("https://www.baidu.com") # get方式访问百度 time.sleep(2) driver.quit() #养成良好习惯,用完关闭浏览器,结束进程
也许在driver = webdriver.Chrome()这步时会报错:“selenium.common.exceptions.WebDriverException: Message: 'geckodriver/chormedriver'”
这里提一下,配置完geckodriver/Chromedriver后,将浏览器安装目录添加到环境path中,重点!!!!!重启pycharm。
既然Selenium用于web应用程序自动化测试的工具,自动化测试三个核心步骤:对象识别,对象操作,断言。
下面我们来看看Selenium的对象识别
1.对象识别:
参考文献:http://www.testclass.net/selenium_python/find-element
Selenium提供了8中定位方法:
- id (页面有id时优先用id)
- name (其次是name)
- class name
- tag name
- link text
- partial link text (链接文本特别长或有特殊字符;链接文本是动态生成,部分内容不确定)
- xpath (万能定位器)
- css selector (万能定位器)
这8种定位方式在Python selenium中所对应的方法为:
- find_element_by_id()
- find_element_by_name()
- find_element_by_class_name()
- find_element_by_tag_name()
- find_element_by_link_text()
- find_element_by_partial_link_text()
- find_element_by_xpath()
- find_element_by_css_selector()
定位方法的用法:
假如我们有一个Web页面,通过前端工具(如,Firebug)查看到一个元素的属性是这样的。
<html> <head> <body link="#0000cc"> <a id="result_logo" href="/" onmousedown="return c({'fm':'tab','tab':'logo'})"> <form id="form" class="fm" name="f" action="/s"> <span class="soutu-btn"></span> <input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">
我们的目的是要定位input标签的输入框。
- 通过id定位:
driver.find_element_by_id("kw")
- 通过name定位:
driver.find_element_by_name("wd")
- 通过class name定位:
driver.find_element_by_class_name("s_ipt")
- 通过tag name定位:
driver.find_element_by_tag_name("input")
- 通过xpath定位,xpath定位有N种写法,这里列几个常用写法:
driver.find_element_by_xpath("//*[@id='kw']") driver.find_element_by_xpath("//*[@name='wd']") driver.find_element_by_xpath("//input[@class='s_ipt']") driver.find_element_by_xpath("/html/body/form/span/input") driver.find_element_by_xpath("//span[@class='soutu-btn']/input") driver.find_element_by_xpath("//form[@id='form']/span/input") driver.find_element_by_xpath("//input[@id='kw' and @name='wd']")
- 通过css定位,css定位有N种写法,这里列几个常用写法:
driver.find_element_by_css_selector("#kw") driver.find_element_by_css_selector("[name=wd]") driver.find_element_by_css_selector(".s_ipt") driver.find_element_by_css_selector("html > body > form > span > input") driver.find_element_by_css_selector("span.soutu-btn> input#kw") driver.find_element_by_css_selector("form#form > span > input")
接下来,我们的页面上有一组文本链接。
<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a> <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
- 通过link text定位:
driver.find_element_by_link_text("新闻") driver.find_element_by_link_text("hao123")
- 通过link text定位:
driver.find_element_by_partial_link_text("新") driver.find_element_by_partial_link_text("hao") driver.find_element_by_partial_link_text("123")
说一说万能的xpath定位方法,在页面中的元素没有唯一标识时,使用xpath方法显得额外简单,xpath方法分为绝对定位和相对定位:
绝对定位顾名思义是从根节点开始计算路径。
相对定位是从指定的节点开始计算路径,当目标元素自身直接定位比较困难,就往上寻找离它最近的可被唯一识别的父元素,然后通过该元素来定位目标元素。
在使用xpath进行定位时,我们可以先使用Chrome或者Firefox进行调试:F12,打开控制台,输入
浙公网安备 33010602011771号